home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / screenview / vms / crudetype.web (.txt) < prev    next >
LaTeX Document  |  1994-04-24  |  118KB  |  2,804 lines

  1. % ADAPTED FROM DVITYPE, VERSION 2.6.
  2. % REVISIONS:
  3. % 9/86: clarify names of global variables, supply hooks for attempted
  4. %           Hewlett-Packard Laserjet version.
  5. % 1/88: Several bugfixes. Chiefly the noscheme bug (TFM files without coding
  6. %       schemes)
  7. %       Also, added some MATH EXTENSION character codes.
  8. % 4/88: Unix change file by P. King.
  9. % 6/88: Changes to produce a "normal" VMS text file by Andrew Trevorrow.
  10. %       All such changes are flagged by AKT comments.
  11. % Here is TeX material that gets inserted after \input webmac
  12. \def\hang{\hangindent 3em\indent\ignorespaces}
  13. \font\ninerm=cmr9
  14. \let\mc=\ninerm % medium caps for names like PASCAL
  15. \def\PASCAL{{\mc PASCAL}}
  16. \def\(#1){} % this is used to make section names sort themselves better
  17. \def\9#1{} % this is used for sort keys in the index
  18. \def\title{Crudetype}
  19. \def\contentspagenumber{1}
  20. \def\topofcontents{\null
  21.   \def\titlepage{F} % include headline on the contents page
  22.   \def\rheader{\mainfont\hfil \contentspagenumber}
  23.   \vfill
  24.   \centerline{\titlefont Crudetype}
  25.   \vskip 50pt
  26.   \centerline{An adaptable device driver (Version 1, 1988)}
  27.   \vskip 50pt
  28.   \centerline{R.M.Damerell,} \vskip 30pt
  29.   \centerline{Mathematics Dept.,} \vskip 20pt
  30.   \centerline{Royal Holloway and Bedford College,} \vskip 20pt
  31.   \centerline{Egham, Surrey, U.K.} \vskip 20pt
  32. \vfill}
  33. \pageno=\contentspagenumber \advance\pageno by 1
  34. % These macros for verbatim scanning are copied from MANMAC.TEX. But we cant
  35. % use the vertical bar for a temporary escape character as WEAVE catches it.
  36. % So we will use ! instead and hope for the best
  37. \chardef\other=12
  38. \def\ttverbatim{\begingroup
  39.   \catcode`\\=\other  \catcode`\{=\other  \catcode`\}=\other  \catcode`\$=\other
  40.   \catcode`\&=\other  \catcode`\#=\other  \catcode`\%=\other  \catcode`\~=\other
  41.   \catcode`\_=\other  \catcode`\^=\other
  42.   \obeyspaces \obeylines \tt}
  43. \outer\def\begintt{$$\let\par=\endgraf \ttverbatim \parskip=0pt
  44.   \catcode`\!=0 \rightskip-5pc \ttfinish}
  45. {\catcode`\!=0 !catcode`!\=\other   % ! is temporary escape character
  46.   !obeylines !obeyspaces    % end of line is active
  47.   !gdef!ttfinish#1^^M#2\endtt{#1!vbox{#2}!endgroup$$}}
  48. \def\up{\hbox{\tt{\char'013}}}
  49. \def\markarrow#1{\vtop{\hbox{#1}\up}}
  50. @* Introduction.
  51. COPYRIGHT ( C ) R.M.Damerell, 1988.
  52. Permission is given to any person to make and distribute copies of this
  53. software, subject to the following conditions:
  54. 1. All copies of the software must carry an exact copy of this notice.
  55. 2. This software is distributed free of charge, "AS IS" with absolutely no
  56. guarantee of performance. Any persons receiving or using this software must do
  57. so entirely at their own risk. Neither the authors nor their institutions
  58. accept any liability for any defects of this software, or for any consequential
  59. loss or damage however caused.
  60. 3. Any person who changes this software must clearly mark it as modified and
  61. add a note describing the changes made.
  62. This is an experimental version and no guarantee of performance is given.
  63. I would like to receive bug reports, same address or electronic mail to
  64. DAMERELL at ARPA.UCL.CS.NSS. \par\vskip 0.5in
  65. This program was originally based on D.E.Knuth's program \.{DVItype}, but so
  66. many changes were needed for various reasons that there is hardly any of the
  67. original code left. The purpose of this program is to provide a framework for
  68. users to write \TeX\ device drivers for a variety of `crude' devices. Roughly
  69. speaking, `crude' means any printer that cannot print the fonts that Metafont
  70. generates. This would include daisy-wheels and most impact dot-matrix
  71. printers. Considered as output printers for \TeX, such devices usually have
  72. some of the following misfeatures: \item
  73. 1. Coarse resolution.\item
  74. 2. Restricted character set. \item
  75. 3. Some printers cannot do reverse line feeds, some can, and tear the paper.
  76. \item
  77. 4. Slow interface between CPU and printer.\par
  78. Although such printers cannot do justice to \TeX\ output, drivers for them
  79. are still needed. Some users cannot afford high quality printers. Some can
  80. only afford to use them for final output; so they need to make proofs on a
  81. cheaper printer. Also, anybody who has a high quality printer may well need
  82. to refer to various \.{WEB} files while writing a driver for it. These can
  83. become illegible in critical places. Here is a sample from \.{DVItype}:
  84. \begintt
  85. A |fix_word| whose respective bytes are $(a,b,c,d)$ represents the number
  86. $$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
  87. b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
  88. -16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
  89. \endtt
  90. Using the basic  (line printer) version of \.{Crudetype}, we can get a copy of
  91. these formulae which is at least legible, even though the result may not be at
  92. all pleasant to look at. A further difficulty with conventional drivers is
  93. that most of these use the algorithm `paint a page of pixels, send it down the
  94. line'. This places a heavy load on both the host computer and the link to the
  95. printer. Of course, one can try to reduce this load by various optimisations,
  96. (e.g. by writing critical bits of code in machine language) but this makes the
  97. program non-portable, and often introduces bugs. \.{Crudetype} is written
  98. entirely in \PASCAL, without any attempt at optimisation. When compiled on a
  99. VAX 780 with the NO-OPTIMISE, CHECK and DEBUG qualifiers it runs at about 2--3
  100. seconds a page. These times are highly variable, and the VMS optimiser reduces
  101. them by about 10-15\%.
  102. @ Printers vary enormously both in their capabilities and in the commands that
  103. drive them. The behaviour of \.{Crudetype} is controlled by a large number of
  104. constants, which supposedly describe how the target printer does things. This
  105. does have the disadvantage that the user must compile a separate copy of the
  106. program for each different printer, and also devise some way to ensure that he
  107. uses the right version for the intended printer. But the only alternative
  108. seemed to be that \.{Crudetype} should read and parse a file describing the
  109. printer and this appeared to be unbearably messy. Ideally, these constants
  110. should be so designed that: \item
  111. 1. Any decent printer can be driven by assigning the right values to these
  112. constants and recompiling. \item
  113. 2. If the printer is properly documented, it should be immediately obvious
  114. what are the correct values for all these constants.
  115. At present I do not have enough experience of different printers to come near
  116. this ideal. In particular, some printers can download characters. The
  117. problems of writing a program to support this facility in proper generality
  118. are horrible and ghastly. I have not made any serious attempt yet to tackle
  119. them. There are just a few places where a hook appears, and I hope eventually
  120. to attach actual routines for downloading.
  121. Some of the more obvious problems of downloading are: when can you download?
  122. At any time? start of page? or only at start of document? Can you load one
  123. character, or must you load a whole font at a time? How much memory does the
  124. printer provide for down loading? How efficiently does it use its memory? What
  125. does it do when it runs out? Can you clear out old fonts to make more space?
  126. What is the format of a down-load command? What parameters does it need, in
  127. what order, with what punctuation? In what order must pixels be sent? Should
  128. they be compressed, and how?
  129. @* Implementation.
  130. The original version of \.{Crudetype} was aimed at a line printer, (because
  131. everybody has these) and was written on the VAX-VMS operating system. The
  132. intention is that this program should be easily adaptable both to other
  133. systems and to other printers. So most of it is written in Standard \PASCAL.
  134. (It is not possible to tell exactly how much of it is Standard, as we do not
  135. have a certified compiler.) But in some places, it is necessary to use
  136. extensions. In particular, \.{Crudetype} must read the font files, whose names
  137. are dynamically specified. That would be impossible in pure \PASCAL.
  138. \.{Crudetype} also uses non-Standard code in order to talk to the user's
  139. terminal. It asks for the name of the \.{DVI} file, and for the first page and
  140. the number of pages to print. If an operating system forbids terminal
  141. interaction, the installer will have to find another way to give the program
  142. this information. As file handling is inevitably system-dependent, I have here
  143. allowed myself a lot of latitude in using VMS-specific procedures. If
  144. \.{Crudetype} cannot find a file, it will ask the user for another name. On
  145. the other hand, all files are read and written sequentially, and I have got
  146. rid of all uses of the default |case| statement. The intention is that all the
  147. system-dependent stuff goes near the top of the file, and all
  148. printer-dependent stuff at the end. Then with any luck you can merely
  149. concatenate Change files for the local system and the local printer, instead
  150. of having to merge them. All the code that is known to be non-Standard has
  151. been carefully segregated from the rest of the program. It amounts to about 20
  152. lines out of 750.
  153. @.System dependencies@>
  154. It is clearly impossible to predict what difficulties will appear in trying
  155. to install \.{Crudetype} on other systems, it would seem to be advisable to
  156. get the line printer version working before trying to adapt it for any other
  157. printer. To try to ease the process, I propose to distribute several test
  158. files with the program. These are of the form SAMPLE.TEX, SAMPLE.DVI and
  159. SAMPLE.PRI (the line printer output).
  160. Although `crude' printers differ very much in their capacities, one thing
  161. they nearly all have in common is that they cannot feed the paper backwards.
  162. Some printers cant |Backfeed| at all; some tear the paper, and others let the
  163. paper slip and so lose position. Therefore it seems to be essential to process
  164. each page as follows: first copy the page into a suitable structure, then sort
  165. it by vertical and horizontal position, then print it.
  166. @* Main Program.
  167. @d banner=='This is Crudetype, Version 1, copyright, experimental'
  168. {printed when the program starts}
  169. @p program crudetype
  170.   @<Declarations@>
  171.   begin
  172.     @<Initialize@>;
  173.     read_BOP;          {AKT: was at start of For each page...}
  174.     repeat
  175.       @<For each page of \.{DVI}, print it if desired@>
  176.     until time_to_stop ;
  177.     @<Clean up afterwards@>;
  178.     666:               {AKT: come here instead of crashing!}
  179.   end.
  180. @ Now here are some of the messy things we must do to satisfy the rules of
  181. \PASCAL.
  182. @<Declarations @>=
  183.   (@<Files @>) ;
  184.   label 666;      {AKT}
  185.   const @< Constants in the outer block @>
  186.   type @< Types in the outer block @>
  187.   var @< Globals in the outer block @>
  188.   @<Forward declarations @>
  189.   @<Lowest level procedures @>
  190.   @<Medium level procedures @>
  191.   @<Top level procedures@>
  192. @ @<Initialize@>=
  193.   @<Set initial values@>
  194.   @<Open terminal channels@>
  195.   @<Assign character codes@>
  196.   @<Dialog with user@>
  197.   @<Open \.{DVI} file@>
  198.   @<Open |printfile|@>
  199.   @<Read \.{DVI} preamble @>
  200. @ @<Glob...@>=
  201.   in_i, in_j :integer; {loop index for initializations}
  202. @ Next, here are some macros for common programming idioms.
  203. @d incr(#) == #:=#+1 {increase a variable by unity}
  204. @d decr(#) == #:=#-1 {decrease a variable by unity}
  205. @d do_nothing == {empty statement}
  206. @d exit == 732
  207. @d return == goto exit
  208.  {Go here when a loop ends abnormally}
  209. @ The next two procedures are very primitive debugging aids.  All internally
  210. detected errors call |error|. Then they can be caught (in VMS) by the debugger
  211. command\begintt
  212.   DBG>set break error \endtt
  213. If a fatal error occurs, then the program will force a crash. With the VMS
  214. debugger, you can then interrogate variables, etc. I chose the square root of
  215. $-1$ as this does not figure prominently in \TeX-related programs.
  216. @^square root@>
  217. @<Forward...@>=
  218. procedure error   ; begin end;
  219. procedure crash;
  220. var u: real;
  221. begin
  222.   goto 666;       {AKT: was
  223.   u := -1 ;
  224.   error;
  225.   u := sqrt(u) ;
  226.                   }
  227. @* Interface to Operating System, 1: Material specific to VAX/VMS.
  228. The purpose of these sections is to try to give a reasonable interface between
  229. the operating system and the rest of the program, which is supposed to be
  230. Standard \PASCAL. Nearly all the non-Standard code is concerned with file
  231. handling and the lowest level of I/O. This is an area where Standard \PASCAL\
  232. seems to be particularly weak. This particular section contains all of the
  233. most system dependent code, and it will probably have to be entirely rewritten
  234. for any other machine. It is hoped that most of the later sections will work
  235. on a wide range of machines. Everything here is system dependent, so there is
  236. no point in indexing each module separately.
  237. @.System dependencies@>
  238. @ The character set. I have here deleted all the code from \.{DVItype} that
  239. translates from characters to small integers and back.  This is because we have
  240. to do a quite different translation anyway. If it is necessary to put that
  241. code back in, then it will probably be necessary to insert \begintt
  242. define zchr(#) == xchr[#] \endtt
  243. because of the different brackets.
  244. @d zchr == chr
  245. @d zord == ord
  246. @ Here are some other system-dependent types. We use double length arithmetic.
  247. The VMS-specific function |dble| converts its argument to double precision.
  248. Integers are 32 bits in VMS. Normally, I use |integer| whenever the bit length
  249. is unimportant, but I use subranges in the |page_record| type, as this allows
  250. packing and may improve the program's performance.
  251. @d real_num == double
  252. @d make_double == dble {convert a |real| to double length}
  253. @d max_half = 32767
  254. @<Types...@>=
  255.   byte = 0..255 ;
  256.   i_word = -max_half-1 .. max_half ;
  257. @ Here we consider the lowest level of file handling. The main input file is
  258. the |dvi_file|. Output for printing goes to |printfile| and diagnostic output
  259. to |term_out|. The terms |display| and |print| are used instead of |write| so
  260. that output may be redirected if desired. Throughout the program, it is
  261. assumed that |@!write| appends its argument(s) to the current record of the
  262. selected file, and |@!write_ln| ends that record and sends it off; this
  263. behaviour is specified in the \PASCAL\ Standard. If these assumptions are
  264. false, it will require major restructuring of the program.
  265. These macros describe how we use the terminal. VMS actually opens the terminal
  266. channels for us, but we get a better style of output by re-opening it, and
  267. using these funny-looking macros to write to it. We can then print a stream of
  268. progress reports without falling foul of a finite record length.
  269. @d term_in==input {the terminal, considered as an input file}
  270. @d term_out==output {and output}
  271. @d i_reset_terminal == do_nothing    {Switch terminal to input}
  272. @d o_rewrite_terminal == do_nothing
  273.   {and back to output. VMS does all this automatically}
  274. @d display(#)==write_ln(term_out, #)
  275. @d display_ln(#)==write_ln(term_out, #, chr(13), chr(10))
  276. @d c_con == @=carriage_control@>
  277. @d warn (#)==begin display_ln('Error: ', #); error; end
  278. @d abort(#)==begin display_ln('Fatal: ', #); crash; end
  279. @d bad_dvi(#)==abort('Bad DVI file: ',# )
  280. @<Files @>= term_in, term_out
  281. @ @<Open terminal channels@>=
  282.   open(term_out, 'SYS$OUTPUT', c_con := none) ;
  283. @ @<Const...@>= can_interact = true ;
  284. @ The printed output goes to |@!printfile|.
  285. @d print(#)==write(printfile, #)
  286. @d print_ln ==write_ln(printfile )
  287. @<Files@>= , printfile
  288. @ @<Glob...@>= printfile : text ;
  289.   print_name: var_string ;
  290. @ In this section we generate a name for the printed file. Essentially, this
  291. involves deleting the `.DVI' at the end of the \.{DVI} filename and adding
  292. `.PRI' instead. But: the \.{DVI} file might be in a funny place, and it might
  293. have a funny extension. Of course, this code all depends crucially on the VMS
  294. file name format, and it will probably be a lot more complicated on systems
  295. that do not allow the elaborate facilities of the VMS |open| command.
  296. @<Open |printfile|@>=
  297.   print_name := dvi_name ;
  298.   chop_tail(print_name, ':') ;
  299.   chop_tail(print_name, ']') ;
  300.   {Chop off directory, disc, and perhaps a logical name}
  301.   chop_top(print_name, '.') ;
  302.   chop_top(print_name, ';') ;
  303.   {and extension and version number}
  304.   append(print_name, print_end) ;     {In VMS, usually  `.PRI'}
  305.   display ('Output is ');             {AKT: was PRINTFILE IS}
  306.   string_show(print_name);
  307.   display_ln(' ') ;
  308.     @.Printfile is...@>
  309. @ VMS \PASCAL\ allows 3 types of carriage control, called |list|, |fortran|,
  310. or |none|. No doubt other systems will have other peculiar types of carriage
  311. control. In VMS, |none| is to be used if at all possible, but some printers
  312. insist on a line feed after every carriage return. Roughly speaking, |@!list|
  313. directs the operating system to put a CR--LF at the end of each record when the
  314. file is printed. |@!fortran| means that a Fortran-type carriage control
  315. character must be put at the start of each record, and \.{Crudetype} assumes
  316. that this must be inserted explicitly. One type of run-time error that causes
  317. a lot of trouble occurs if you try to write too many characters onto one
  318. record of the |printfile|. I have tried to defeat this by declaring a very
  319. long record length.
  320. @ @d r_len == @=record_length@>
  321. @<Open |printfile|@>=
  322.   if fortran then
  323.   open (printfile, print_name.data, c_con := fortran, r_len := 30000 )
  324.   else if list then
  325.   open(printfile, print_name.data, c_con := list, r_len := 30000 )
  326.   else
  327.   open(printfile, print_name.data, c_con := none, r_len := 30000 ) ;
  328.   rewrite(printfile) ;
  329. @ Now here is the lowest-level procedure for opening binary files. This will
  330. have to be rewritten to run on any other system. The VMS |open| procedure
  331. tries to open the file with the given name; if bits of the name are missing,
  332. it can obtain them from the |default| parameter. It generates a non-zero
  333. |status| if it fails.
  334. @d close_binary(#)==
  335.     close(# , @=error := continue@> )
  336. @<Lowest...@>=
  337.   function open_binary
  338.   (var f_f: byte_file; name: var_string; other_name: def_name_type): boolean;
  339.   var s: integer;
  340.   begin
  341.     close_binary(f_f );
  342.       {in case the file was left open}
  343.     open(f_f, name.data , readonly, ,, fixed, default := other_name,
  344.       @=error := continue@> );
  345.     s := status(f_f) ;
  346.     if s <> 0 then open_binary := false
  347.     else begin
  348.       reset(f_f , @=error := continue@> );
  349.       s := status(f_f) ;
  350.       open_binary := (s = 0 );
  351.     end;
  352.   end;
  353. @ Here we define system-dependent properties of these files. The easiest way
  354. to tell VMS where to look for files is by giving them default names. These
  355. should all be the same length if possible.
  356. @d block_length = 512
  357. @<Const...@>=
  358.   dvi_def =   '          *.DVI' ;
  359.   tfm_def =   'TEX$FONTS:*.TFM' ;
  360.   pxl_def =   'TEX$PIXLDIR:*.*' ;
  361. @ @<Types...@>= def_name_type = packed array [1..15] of char ;
  362. @ Here are macros for the adaptable merge sort. See the section on sorting for
  363. explanation.
  364. @d image(#) == pool[#]
  365. @d create == incr(cell)
  366. @d link_type == page_i
  367. @d first_cell == cell := 0
  368. @d wipe_out(#) ==
  369. @d declare_pool ==  pool: array [page_i] of page_record;
  370. @d garbage == cell := zzz ;
  371. @ These upper bounds are put in to catch runaway arguments.
  372. @<Const...@>=
  373.   page_max = 10000 ;
  374.   max_line_size = 1000;
  375. @* Interface to Operating System, 2: Terminal input and output.
  376. When \.{DVItype} begins, it engages the user in a brief dialog so that the
  377. options will be specified. This version of \.{Crudetype} does the same. This
  378. requires nonstandard \PASCAL\ constructions to handle the online interaction.
  379. So it may be necessary on some systems to omit the dialog. If so, the
  380. installer must find some way to get the \.{DVI} file name into the |@!buffer|.
  381. @<Dialog...@>=
  382.   {AKT: removed display_ln(banner, ' --- ', device_ID) ;}
  383.   buffer := blank ;
  384.   repeat
  385.     ask_prompt('DVI file name? ');
  386.     dvi_name := buffer ;
  387.   until dvi_name.len > 0 ;
  388.   ask_prompt('First page? (default = 0) ' );
  389.   first_page := get_number(0 ) ;
  390.   ask_prompt('maximum no. of pages? (default = 1000000) ' ) ;
  391.   max_pages := get_number(1000000) ;
  392.     @.DVI file name?@>@.First page?@>@.max. no. of pages?@>
  393. @ Most characters in \TeX\ fonts are narrower than line-printer characters. So
  394. we must spread them out to make them fit. Originally, this was done by
  395. multiplying \.{DVI} distances by a constant factor |h_fudge|. This is all
  396. right for one size of type but it tends to fail for other sizes because if the
  397. predominant type size is larger than expected, then rounding with a constant
  398. factor makes everything\qquad\ very\qquad\ badly\qquad\ spread\qquad\ out. It
  399. seemed that the least bad way to tackle this is to allow the user to specify
  400. an extra magnification factor.
  401. @<Dialog...@>=
  402.   {AKT: removed
  403.   if can_interact then display_ln(
  404.     'What magnification?  This must be an integer, and is a percentage');
  405.   ask_prompt ( 'Default = 100% = DVI file magnification ') ;
  406.   extra_mag := get_number(100)/100.0 ;
  407.     @.What magnification?@>
  408. @ Since the terminal is being used for both input and output, some systems
  409. need a special routine to make sure that the user can see a prompt message
  410. before waiting for input based on that message. (Otherwise the message may
  411. just be sitting in a hidden buffer somewhere, and the user will have no idea
  412. what the program is waiting for.) Here, we assume that the system-dependent
  413. macro |@!i_reset_terminal| (defined above) will do whatever is necessary to
  414. switch the terminal from output to input. Likewise, |@!o_rewrite_terminal|
  415. must switch it from input to output. Note that the program assumes that the
  416. terminal is normally in output mode, and explicitly calls these macros when it
  417. wants input. If the system does not allow this, then |@!can_interact| should
  418. be set false.
  419. Here is how the program prompts for input: the argument of |ask_prompt| is the
  420. prompt text. Because of the anomalous behaviour of |write|, this ought to work
  421. with arguments of any length, even on versions of \PASCAL\ that only allow
  422. fixed length strings.
  423. @d ask_prompt(#) == if can_interact then begin
  424.   {AKT: removed display_ln(#) ;} read_terminal ; end;
  425. @<Lowest...@>=
  426.   procedure read_terminal;
  427.   var k: byte ;
  428.   begin i_reset_terminal;
  429.     buffer := blank ;
  430.     if not eof(term_in ) then begin
  431.       if eoln(term_in) then read_ln(term_in);
  432.       k:=0;
  433.       while not eoln(term_in) do
  434.       begin incr(k); buffer.data[k]:=term_in^; get(term_in);
  435.       end;
  436.       buffer.len := k ;
  437.       upcase(buffer) ;
  438.     end;
  439.     o_rewrite_terminal ;
  440.   end;
  441. @ @<Glob...@>=
  442.   @!buffer: var_string ;  {for terminal input}
  443.   @!extra_mag:real_num ;
  444. @ The next function reads an integer from the |buffer|. It assumes a previous
  445. call of |ask_prompt| and returns the default if the input is unrecognisable.
  446. BODGE: this cant handle negative numbers.
  447. @<Lowest...@>=
  448.   function get_number(default: integer): integer;
  449.   var k, m : integer; c:byte ;
  450.   begin
  451.     k := 0 ;
  452.     repeat
  453.       incr(k) ;
  454.       c := zord(buffer.data[k] ) ;
  455.     until (k > buffer.len)
  456.         or ((c <> " " ) and (c <> "+" ) ) ;
  457.     if (k > buffer.len) or (c < "0") or (c > "9" )
  458.     then get_number := default
  459.     else
  460.     begin
  461.       m:=0;
  462.       while (c >="0") and (c <="9") do begin
  463.         m:=10*m+ c -"0"; incr(k);
  464.         c := zord(buffer.data[k] ) ;
  465.       end;
  466.       get_number := m ;
  467.     end;
  468.   end;
  469. @ If the printer is actually a VDU, then possibly the user will want to pause
  470. at intervals.
  471. @<Check pause@>=
  472.   if can_interact and do_pause and (PR_v >= next_pause) then begin
  473.     display_ln(pause_ask);
  474.     i_reset_terminal;
  475.     read_ln (term_in );
  476.     o_rewrite_terminal ;
  477.     string_show(pause_after);
  478.     next_pause := next_pause + pause_steps ;
  479.   end;
  480. @ @<Pause reset@>=
  481.   if do_pause then
  482.   begin next_pause := pause_steps; @<Check pause@> end;
  483. @ @<Const...@>=
  484.  @<Pause constants, to be defined by the printer change file@>
  485. @ @<Glob...@>=
  486.   next_pause: integer;
  487.   pause_after: var_string ;
  488. @* Interface to Operating System, 3: Input from binary files.
  489. The main input file is the \.{DVI} file. Logically, this is just a stream of
  490. 8-bit bytes, with no record or block structure. However VMS \PASCAL\
  491. apparently cannot handle files of this type; so I have adopted the blocking
  492. scheme (due to D.R.Fuchs) from the VMS \.{DVItype} change file. But a lot of
  493. the code has been rewritten. Some other operating systems use similar
  494. blocking schemes; so this code may possibly work without much change. The
  495. program deals with two binary file variables: |@!dvi_file| is the main input
  496. file that we are printing, and |@!tfm_file| the current font metric file from
  497. which character-width information is being read. Each of these has a name and
  498. a counter, declared here; also a default name (system dependent, and so
  499. declared previously). As an initial attempt at downloading, we declare a
  500. |@!pxl_file|.
  501. @^Fuchs, D.R.@>
  502. @<Files...@>= , dvi_file, tfm_file, pxl_file
  503. @ @<Glob...@>=
  504.   dvi_file, tfm_file, pxl_file: byte_file ;
  505.   dvi_name, tfm_name, pxl_name: var_string ;
  506.   dvi_indx, tfm_indx, pxl_indx: integer ;
  507.   font_ok: boolean ;
  508. @ @<Types...@>=
  509.   @!byte_block=packed array [0..block_length-1] of byte ;
  510.   @!byte_file= packed file of byte_block;
  511. @ This code opens the \.{DVI} file; clearly, a failure is fatal.
  512. @<Open \.{DVI} file@>=
  513.   {AKT: removed display_ln ('Opening DVI file ' );}
  514.   if not open_and_ask(dvi_file, dvi_indx, dvi_name, dvi_def)
  515.   then abort('Could not open DVI file!');
  516.     @.Fatal: couldnt open@>@.Opening DVI file@>
  517. @ But when we come to open a font file, we merely report a failure:
  518. @<Open font file@>=
  519.   font_ok := open_and_ask (tfm_file, tfm_indx, tfm_name, tfm_def) ;
  520. @ Here is the procedure that actually opens files. It searches for a file
  521. called |name|, supplying missing bits from the default file-specification in
  522. |other_name|. |f_f| is the file being opened, and |f_c| is its counter.
  523. @<Medium...@>=
  524.   function open_and_ask
  525.   (var f_f: byte_file; var f_c: integer; var name: var_string;
  526.     other_name: def_name_type ) : boolean ;
  527.   label exit ;
  528.   var success : boolean;
  529.   begin
  530.     success := false;
  531.     repeat
  532.       success := open_binary(f_f, name, other_name) ;
  533.       if success then f_c := 0
  534.       else
  535.       @<Try to get an alternative name, |return| if this fails@>
  536.     until success ;
  537.     exit: open_and_ask:= success ;
  538.   end;
  539. @ If this fails, then ask the user for another name. If the operating system
  540. forbids this, or if the user refuses, then return |false| to indicate failure.
  541. @<Try to get...@>=
  542.   begin
  543.     return;    {AKT: don't ask user for another name}
  544.     warn ('Couldnt open file, search name was, ' );
  545.     string_show(name) ;
  546.     display_ln (' ') ;
  547.     display_ln  ('default name was  ' , other_name );
  548.     if can_interact then begin
  549.       ask_prompt('Please type a replacement or NO to abandon search' ) ;
  550.       name := buffer ;
  551.       if (name.len = 2) and
  552.       (name.data[1] = 'N') and (name.data[2] = 'O')
  553.       then return;
  554.     end else return;
  555.   end ;
  556.     @.error: couldnt open@>@.Please type...@>
  557. @ \.{DVItype} has seven functions for reading integers from the \.{DVI} file
  558. and two more for the \.{TFM} file. I have condensed these. In order for
  559. these procedures to work, they must all have as parameters, both the file and
  560. its attached counter. These macros generate the procedure calls.
  561. @d read_end(#) == # @=)@>
  562. @d skip(#) == skip_bytes @=(@> # @& file, # @& indx, read_end
  563. @d get_integer(#) == read_integer @=(@> # @& file, # @& indx, read_end
  564. @d get_byte(#) == read_byte(# @& file, # @& indx)
  565. @d get_real(#) == read_real(# @& file, # @& indx)
  566. @<Lowest...@>=
  567.   function read_byte(var f_file: byte_file; var f_indx: integer) : byte;
  568.   begin
  569.     if eof(f_file) then
  570.     warn('fallen off end of file' )
  571.       @.error: fallen off...@>
  572.     else begin
  573.       read_byte := f_file^[f_indx] ;
  574.       incr(f_indx);
  575.       if f_indx =block_length then begin
  576.         get(f_file );
  577.         f_indx:=0;
  578.       end;
  579.     end;
  580.   end ;
  581.   procedure skip_bytes(var f_file: byte_file; var f_indx: integer; n:integer);
  582.   {discard n bytes from |f_file|}
  583.   begin
  584.     if n < 0 then abort('skip_bytes called with negative number');
  585.     f_indx := f_indx + n;
  586.     while f_indx >= block_length do
  587.     begin
  588.       if eof(f_file) then
  589.       warn('fallen off end of file' )
  590.       else get(f_file );
  591.       f_indx := f_indx - block_length ;
  592.     end ;
  593.   end;
  594.     @.error: fallen off...@> @.Fatal: skip_bytes called...@>
  595. @ The next function reads an integer from a file. |k| specifies the type.
  596. |abs(k)| is the number of bytes, and the integer will be signed if |k<0|.
  597. @<Lowest...@>=
  598.   function read_integer
  599.     (var f_file: byte_file; var f_indx: integer; k: integer): integer;
  600.     var a, i : byte; n: integer;
  601.     begin n := get_byte(f );
  602.       if (k < 0) and (n > 127) then n := n-256 ;
  603.       for i := 1 to abs(k) - 1 do
  604.       begin
  605.         a := get_byte(f ) ;
  606.         n := n*256 + a ;
  607.       end ;
  608.       read_integer := n ;
  609.     end;
  610. @ A real number is stored in the file as 2 integers, numerator first.
  611. @<Medium...@>=
  612.   function read_real(var f_file: byte_file; var f_indx: integer ): real_num;
  613.   var a, b: integer;
  614.   begin a := get_integer(f )(-4);
  615.     b :=  get_integer(f )(-4);
  616.     if b <= 0 then
  617.     begin
  618.       warn('denominator <= 0! '); read_real:= 1;
  619.     end
  620.     else read_real:= make_double(a)/make_double(b) ;
  621.   end;
  622.     @.error: denominator...@>
  623. @* Page selection.
  624. We have now disposed of all the code that is known to be system-dependent, so
  625. we can resume a proper top-down description of the program. The basic method
  626. for processing each page is that all printable characters are written onto a
  627. structure called a `page image'. This is a list of things called `page
  628. records'. Each page record represents one printable character, and contains
  629. two fields giving the intended position on the page. Eventually the image will
  630. be sorted and then copied to the |printfile|. This means that \.{Crudetype}
  631. has to remember three sets of coordinates. In order to help to keep track of
  632. many global variables, we use prefixes. \.{DVI} variables are prefixed with
  633. |D_|, page image variables with |IM_|, and the printer's variables with |PR_|.
  634. When this module starts, the \.{DVI} file should be positioned at or before a
  635. @<For each page...@>=
  636.   {AKT: moved first read_BOP before repeat loop}
  637.   if (counter[0] >= first_page) then start := true ;
  638.      {AKT: what if counter[0] is < 0???}
  639.   if start and (max_pages > 0 )
  640.   then begin
  641.     decr(max_pages);
  642.     display('[', counter[0]:1, ']' ); {Progress report}
  643.     Read_one_page ;
  644.     @<Sort the page@>
  645.     Send_page ;
  646.     {AKT: removed @<Formfeed@>;}
  647.   end
  648.   else if max_pages > 0 then Skip_page
  649.   else time_to_stop := true;
  650.   {AKT: only call Formfeed BETWEEN pages; this requires some hackery}
  651.   read_BOP;                           {sets max_pages to -1 if no more}
  652.   time_to_stop := max_pages <= 0;
  653.   if (not time_to_stop) and start then begin
  654.      @<Formfeed@>;
  655.   end;
  656. @ This program only gives a small subset of the page-selection facilities of
  657. \.{DVItype}. The most you can do is to specify the starting page and the
  658. maximum number of pages to print. This will be controlled by these variables:
  659. @<Glob...@>=
  660.   start, time_to_stop: boolean;
  661.   first_page,  max_pages: integer;
  662.   counter: array[0..9] of integer ;
  663. @ @<Set init...@>=
  664.   start := false ; time_to_stop := false;
  665.   for in_i := 0 to 9 do counter[ in_i ] := 0 ;
  666. @ |@!D_com| is the \.{DVI} command byte, |@!D_par| its first parameter.
  667. @<Top level...@>=
  668.   procedure Read_one_page ;
  669.   var D_com: byte; D_par: integer; end_page: boolean ;
  670.   begin end_page := false;
  671.     @<Set up an empty page image@>
  672.     repeat
  673.       @<Get \.{DVI} command |D_com|, and do it@>
  674.     until end_page;
  675.   end ;
  676. @#procedure Skip_page ;
  677.   var D_com: byte; D_par: integer; end_page: boolean ;
  678.   begin
  679.     end_page := false;
  680.     repeat
  681.       @<Skip \.{DVI} command, but we must process any |font_def|@>
  682.     until end_page;
  683.   end ;
  684. @* Translating the device-independent file, 1: The big switch.
  685. Refer to \.{DVItype} or to \.{TUG}boat (Vol.3, No.2) for a description of the
  686. \.{DVI} file format. As in \.{DVItype}, we process each \.{DVI} command via a
  687. big |case| statement. But 192 of the cases are very similar, so lets dispose
  688. of them first.
  689.  @.TUGboat@>
  690. @d id_byte=2 {identifies the kind of \.{DVI} files described here}
  691. @d move_right ==
  692.     D_h := D_h + D_dis ;
  693.     IM_h := IM_h + IM_dis
  694. @<Get \.{DVI} command...@>=
  695.   D_com := get_byte(dvi);
  696.   if D_com < 128 then begin
  697.     set_character(D_com); move_right ;
  698.   end
  699.   else if (D_com >= 171) and (D_com <= 234) then
  700.     change_font(D_com - 171)
  701.   else
  702. @ @<Skip \.{DVI} command...@>=
  703.   D_com := get_byte(dvi);
  704.   if (D_com < 128)
  705.   or ((D_com <= 234) and (D_com >= 171))
  706.   then do_nothing
  707.   else
  708. @ Now we come to the |case| statement proper. This section of the program is
  709. long and complicated, and I have tried to clean it up. Some commands want an
  710. unsigned parameter, called |D_par|, to be read from the file. We use
  711. |four_cases| for those. Others want a signed parameter; they are all
  712. movements. We use |move_cases| for those.
  713. @d four_case_end(#) == # ; end
  714. @d four_cases(#)==
  715.   #,#+1,#+2,#+3: begin D_par := get_integer(dvi)( D_com - # + 1 );
  716.     four_case_end
  717. @d move_cases(#)==
  718.   #,#+1,#+2,#+3: begin D_par := get_integer(dvi)( # - D_com - 1 );
  719.     four_case_end
  720. @<Get \.{DVI} command...@>=
  721. case D_com of
  722.   four_cases(128)    (set_character(D_par); move_right );
  723.   132:                begin set_rule; move_right ; end;
  724.   four_cases(133)    (set_character(D_par) );
  725.   137:                set_rule ;
  726.   138:                do_nothing ;
  727.   140:                end_page := true ;
  728.   141:                push;
  729.   142:                pop;
  730.   move_cases(143)    (D_h := D_h+D_par);
  731.   147:{W0}            D_h := D_h+D_w ;
  732.   move_cases(148)    (D_w := D_par; D_h := D_h+D_w );
  733.   152:{X0}            D_h := D_h+D_x ;
  734.   move_cases(153)    (D_x := D_par; D_h := D_h+D_x );
  735.   move_cases(157)    (move_down(D_par));
  736.   161:{Y0}            move_down(D_y);
  737.   move_cases(162)    (D_y := D_par; move_down(D_y) );
  738.   166:{Z0}            move_down(D_z);
  739.   move_cases(167)    (D_z := D_par; move_down(D_z) );
  740.   four_cases(235)    (change_font(D_par) );
  741.   four_cases(243)    (define_font(D_par) );
  742.   @<Fourteen illegal cases: print suitable error messages@>
  743. end ;
  744. @ When skipping a page, we must throw away parameters instead of using them.
  745. @d four_throw(#) ==
  746.   #,#+1,#+2,#+3: skip(dvi)(D_com - # + 1 )
  747. @<Skip \.{DVI} command...@>=
  748. case D_com of
  749.   four_throw(128);
  750.   132, 137: skip(dvi)(8); {sizes of a rule}
  751.   four_throw(133);
  752.   138:                ;
  753.   140:                end_page := true ;
  754.   141,142:            ;
  755.   four_throw(143);
  756.   147:                ;
  757.   four_throw(148);
  758.   152:                ;
  759.   four_throw(153);
  760.   four_throw(157);
  761.   161:                ;
  762.   four_throw(162);
  763.   166:                ;
  764.   four_throw(167);
  765.   four_throw(235);
  766.   four_cases(243)    (define_font(D_par) );
  767. @<Fourteen illegal...@>
  768. end ;
  769. @ Finally, there are 14 illegal values of |D_com| that generate various errors.
  770. @<Fourteen illegal...@>=
  771.   four_cases(239)
  772.     ({AKT: removed warn('ignoring \special') ;} skip(dvi)(D_par) );
  773.   139, 247, 248, 249:
  774.     bad_dvi('byte: ', D_com:1 , ' out of context inside page' ) ;
  775.   250,251,252,253,254,255:
  776.     bad_dvi('Illegal command byte, ', D_com ) ;
  777.       @.error: cant do xxx@>
  778.       @.Fatal: Bad DVI file@>
  779. @* Translating the device-independent file, 2: Paging and the stack.
  780. The definition of \.{DVI} files refers to six registers, (|D_h, D_v, D_w,
  781. D_x, D_y, D_z|), which hold integer values in \.{DVI} units. We shall need
  782. additional registers in order to calculate a rounded position. From time to
  783. time, we save the current values of these on a stack, represented by the
  784. following arrays.
  785. @d max_stack = 100 {\.{DVI} files shouldn't |push| beyond this depth}
  786. @<Glob...@>=
  787.   D_h,D_v,D_w,D_x,D_y,D_z : integer;            {current \.{DVI} state values}
  788.   D_h_stack, D_v_stack, D_w_stack, D_x_stack, D_y_stack, D_z_stack:
  789.     array [0..max_stack+2] of integer; {pushed down values }
  790.   @!stack_ht: 0..max_stack;    {current stack depth}
  791.   just_pushed: boolean;
  792. @ @<Set up an empty page image@>=
  793.   D_h := 0 ; D_v := 0 ;
  794.   D_w := 0 ; D_x := 0 ;
  795.   D_y := 0 ; D_z := 0 ;
  796.   stack_ht := 0 ;
  797.   rail_base := 0 ;
  798.   just_pushed := false ;
  799. @ Here is how \.{DVI}type manipulates the stack: The first |push| on a page
  800. fills the zeroth place on the stack and sets |stack_ht| = 1. So the used
  801. places are numbered |0..stack_ht- 1|. Now |push| and |pop| do the obvious
  802. things.
  803. @<Lowest...@>=
  804.   procedure push;
  805.   var x: real_num ;
  806.   begin if stack_ht=max_stack then
  807.     warn('Capacity exceeded (stack size=', max_stack:1,')')
  808.     else begin
  809.       D_h_stack[stack_ht]:=D_h; D_v_stack[stack_ht]:=D_v;
  810.       D_w_stack[stack_ht]:=D_w; D_x_stack[stack_ht]:=D_x;
  811.       D_y_stack[stack_ht]:=D_y; D_z_stack[stack_ht]:=D_z;
  812.       @<Some adjustments are needed here for rounding@>
  813.       incr(stack_ht); just_pushed := true ;
  814.     end;
  815.   end;
  816.     @.error: Capacity exceeded @>
  817. @# procedure pop;
  818.   begin if stack_ht=0 then warn('POP illegal at level zero')
  819.     else  begin
  820.       decr(stack_ht);
  821.       D_h:=D_h_stack[stack_ht]; D_v:=D_v_stack[stack_ht];
  822.       D_w:=D_w_stack[stack_ht]; D_x:=D_x_stack[stack_ht];
  823.       D_y:=D_y_stack[stack_ht]; D_z:=D_z_stack[stack_ht];
  824.       IM_h := IM_h_stack[stack_ht];IM_v := IM_v_stack[stack_ht];
  825.       @<Set |rail_base|@>
  826.     end;
  827.   end;
  828.     @.error: POP illegal...@>
  829. @ This procedure gets called when we expect to read a new page. It looks for
  830. the next |BOP|; if it finds the postamble instead, it sets |max_pages < 0| as
  831. a signal.
  832. @d POST = 248
  833. @d NOP = 138
  834. @d BOP = 139
  835. @<Top level...@>=
  836.   procedure read_BOP;
  837.   var k: byte ; D_par:integer ;
  838.   begin
  839.     repeat k:= get_byte(dvi);
  840.       if (k>= 243)and(k <= 246 ) then {a |font_def|}
  841.       begin D_par:=get_integer(dvi) (k-242 ); define_font(D_par); k:=NOP;
  842.       end;
  843.     until k<>NOP;
  844.     if k=POST then
  845.     max_pages := -1
  846.     else if k<>BOP then bad_dvi('byte is not BOP')
  847.       @.Fatal: Bad DVI file@>
  848.     else begin
  849.       for k:=0 to 9 do counter[k]:= get_integer(dvi)(-4);
  850.       skip(dvi)(4);
  851.     end;
  852.   end;
  853. @ A \.{DVI}-reading program that reads the postamble first need not look at the
  854. preamble; but \.{Crudetype} reads the \.{DVI} file sequentially.
  855. @d PRE=247 {preamble}
  856. @<Read \.{DVI} preamble@>=
  857.   bbb:= get_byte(dvi); {fetch the first byte}
  858.   if bbb<>PRE then bad_dvi('First byte isn''t start of preamble!');
  859.     @.Fatal: Bad DVI file@>
  860.   bbb:= get_byte(dvi); {fetch the identification byte}
  861.   if bbb<>id_byte then
  862.   warn('identification byte should be ',id_byte:1,', it is actually', bbb:1 );
  863.     @.error: identification...@>
  864.   @<Compute the conversion factors@>;
  865.   bbb:= get_byte(dvi); {fetch the length of the introductory comment}
  866.   {AKT: removed display(' ');}
  867.   for nnn := 1 to bbb do
  868.       {AKT: was display(zchr(get_byte(dvi)));}
  869.       bbb := get_byte(dvi);
  870.   {AKT: removed display_ln(' ');}
  871. @ The conversion factor |h_conv| is figured as follows: There are exactly
  872. |n/d| decimicrons per \.{DVI} unit and 254000 decimicrons per inch, and
  873. |h_resolution| |h_steps| per inch.
  874. @<Glob...@>=
  875.   dvi_factor, h_conv, v_conv, magnification : real_num;
  876.   nnn:integer; {general purpose register}
  877.   bbb: byte ;
  878. @ @<Compute the conversion factors@>=
  879.   dvi_factor := get_real(dvi)/254000.0 ;
  880.   magnification :=  get_integer(dvi)(4) / 1000 ;
  881.   dvi_factor := dvi_factor * magnification ;
  882.     {This converts \.{DVI} units to inches (on an ideal device) }
  883.   h_conv:= dvi_factor * h_resolution * h_fudge * extra_mag;
  884.   v_conv:= dvi_factor * v_resolution * v_fudge * extra_mag ;
  885. @* Translating the device-independent file, 3: Setting a Rule.
  886. |D_p| is the height and |D_q| is the width. A rule has to be assembled from
  887. the available characters. First: is the rule to be set at all? Second: is it
  888. horizontal or vertical? (Because of the limited name lengths, we call them
  889. |Post| and |Rail|.) The test applied here is quite arbitrary.
  890. @<Medium...@>=
  891.   procedure set_rule;
  892.   var D_p,D_q: integer;
  893.   begin
  894.     D_p:=get_integer(dvi) (-4);
  895.     D_q:=get_integer(dvi)(-4);
  896.     if (D_p<=0)or(D_q<=0) then
  897.       {an invisible rule! Dont ask me why \TeX\ wants to do this}
  898.     else if D_p*v_conv <= post_height/2
  899.     then do_rail(D_p, D_q)
  900.     else do_post(D_p, D_q);
  901.   end;
  902. @ Setting a vertical rule is simple: we just fill all the space with the
  903. relevant character.
  904. @<Lowest...@>=
  905.   procedure do_post(D_rul_ht, D_rul_width: integer);
  906.   var vn, vi, hn, hi, post_v, rule_hp : integer;
  907.   rule_cod: code_object ;
  908.   begin
  909.     @<|Post| set sizes@>;
  910.     for vi := vn - 1 downto 0 do
  911.     begin
  912.       post_v := IM_v - vi * post_height ;
  913.       for hi := 1 to hn do
  914.       begin
  915.         rule_hp := IM_h + (hi - 1) * post_width ;
  916.         do_set_char(post_v, rule_hp, rule_cod);
  917.       end;
  918.     end;
  919.   end;
  920. @ Note that whereas \.{DVItype} rounds all sizes up, \.{Crudetype} rounds to
  921. nearest integer.  This seems more likely to work on a crude resolution.
  922. But we force the rounded size to be |>= 1| .
  923. @<|Post| set...@>=
  924.   round_IM_h ( 0);
  925.   hn := round(D_rul_width * h_conv / post_width );
  926.   vn := round(D_rul_ht * v_conv / post_height);
  927.   if hn <= 0 then hn := 1;
  928.   if vn <= 0 then vn := 1;
  929.   rule_cod := post_char;
  930. @ A horizontal rule is more complicated, as there is then a selection of
  931. characters. This matters if the printer has only a very coarse vertical
  932. positioning. For example, a line printer has only minus and underscore, but a
  933. VT-100 has 5 bars at different heights. |@!rail_types| should be set to the
  934. number of different horizontal bars that the printer can draw within one
  935. |v_step|. We measure the vertical position of a rule in |rail_steps|,
  936. which are smaller than |v_steps| in the same ratio.
  937. @<Glob...@>=
  938.   rail_chars : packed array [1..rail_types] of code_object ;
  939.     {Number from bottom of page up; so no. 1 might be an underscore}
  940.   rail_base : integer ;
  941.     {Position of bottom edge of a  rule in |rail_steps|}
  942.   post_char : code_object ;
  943. @ @<Const...@>=
  944.   @<Rule setting constants@>
  945.   {Printer-dependent, so they must go at the end of the file}
  946. @ @<Lowest...@>=
  947.   procedure do_rail(D_rul_ht, D_rul_width: integer);
  948.   var vn, vi, hn, hi,
  949.   rail_v,  {Current position in |rail_steps|}
  950.   char_vp,  {Position in |v_steps| where a rule char will be set}
  951.   rule_hp: integer;
  952.   rule_cod: code_object ;
  953.   char_i : 1..rail_types ; {indicates which character to be used}
  954.   begin
  955.     @<|Rail| set sizes@>
  956.     for vi := vn-1 downto  0 do begin
  957.       rail_v := rail_base - vi ;
  958. @ Now to assign |char_i| and |char_vp|. The easiest way is to consider a simple
  959. example. Suppose |rail_types = 5| and |rail_v = 50|. This addresses the
  960. underscore at the bottom edge of a text character at |10 v_steps|.
  961. So |char_i| wants to be 1 and |char_vp| 10. So...
  962. @<Lowest...@>=
  963.   char_vp := ((rail_v - 1) div rail_types ) + 1 ;
  964.   char_i :=  rail_types - ((rail_v - 1) mod rail_types ) ;
  965.   rule_cod := rail_chars [ char_i] ;
  966.   for hi := 1 to hn do begin
  967.     rule_hp := IM_h + (hi-1) * rail_width ;
  968.     do_set_char(char_vp, rule_hp, rule_cod) ;
  969.   end;
  970. @ @<|Rail| set...@>=
  971.   round_IM_h ( 0);
  972.   hn := round(D_rul_width * h_conv/ rail_width);
  973.   vn := round(D_rul_ht * v_conv * rail_types/ rail_height );
  974.   if hn <= 0 then hn := 1;
  975.   if vn <= 0 then vn := 1;
  976. @ Now consider how to set |rail_base|. Horizontal rules are mostly used for
  977. underlining text, and then they should be aligned with the underscore
  978. character on the same line of text. So normally, we just do the following. The
  979. exception occurs when the \.{DVI} file does an explicit vertical move.
  980. @<Set |rail_base|@>=
  981.   rail_base := IM_v * rail_types ;
  982. @* Translating the device-independent file, 4: Changing and defining Fonts.
  983. The following tables describe all the \TeX\ fonts that \.{Crudetype}
  984. currently knows about.
  985. @<Glob...@>=
  986.   nf: D_font_ptr ;
  987.     {The number of fonts so far defined. These will be numbered |0..nf-1| }
  988.   @!font_num,         {external font numbers}
  989.   @!font_space,       {boundary between ``small'' and ``large'' spaces}
  990.   @!scheme,           {pointer to coding scheme}
  991.   @!first_ch,         {First character in the font}
  992.   @!last_ch:          {and last}
  993.     array [D_font_ptr] of integer;
  994.   D_width: array[D_font_ptr, D_char_ptr ] of integer ;
  995.     {character widths, as given in \.{TFM} file, should be in \.{DVI} units}
  996.   @!D_check,     {the font checksum must be global for HPGF}
  997.   thin_space, D_font, cur_scheme: integer ;     {The current values}
  998. @ @<Type...@>=
  999.   D_font_ptr = 0..max_D_fonts;
  1000.   D_char_ptr = 0..max_D_char;
  1001. @ The size of the tables can be altered at compile time to extend or reduce
  1002. \.{Crudetype}'s capacity.
  1003. @<Constants...@>=
  1004.   @!max_D_fonts=100; {maximum number of distinct fonts per \.{DVI} file}
  1005.   @!max_D_char =255; {AKT: was 127 but we want to handle PostScript fonts}
  1006. @ Initially, all these tables are blank.
  1007. @<Set init...@>=
  1008.   nf:=0;
  1009.   for in_i := 0 to max_D_fonts do
  1010.   begin
  1011.     font_num[in_i ] := 0 ;
  1012.     scheme[in_i ] := 0 ;
  1013.     first_ch[in_i ] := 0 ;
  1014.     last_ch[in_i ] := 0 ;
  1015.     font_space[in_i]:= 0 ;
  1016.   end;
  1017. @ @<Set up an empty page image@>=
  1018.   D_font := nf  ;
  1019.   cur_scheme := 0 ;
  1020. @ @<Medium...@>=
  1021.   procedure change_font (D_new: integer);
  1022.   begin
  1023.     D_font := 0 ;
  1024.     font_num[nf]:=D_new;
  1025.     while font_num[D_font]<>D_new do incr(D_font);
  1026.     if D_font = nf then
  1027.     warn('Undefined font called for, number ', D_new:1 );
  1028.       @.error: Undefined font@>
  1029.     cur_scheme := scheme[D_font] ;
  1030.     thin_space := font_space[D_font] ;
  1031.   end;
  1032. @ The following procedure is called whenever we read a |font_def| command from
  1033. the \.{DVI} file. In general, any error while defining a font causes a jump to
  1034. label |bad_font|, leaving the new font undefined.
  1035. @d bad_font = 9999
  1036. @d good_font = 9998
  1037. @d font_error(#) == begin
  1038.   warn(#); display_ln('font number ', D_new:1, ' cannot be loaded') ;
  1039.   goto bad_font ;
  1040. @<Medium...@>=
  1041.   procedure define_font (D_new:integer );
  1042.   label bad_font , good_font ;
  1043.   var @<|font_def| vars@>
  1044.   begin
  1045.     @<Read the font parameters from the \.{DVI} file,
  1046.       calculate scaling factors@>;
  1047.     @<Try to load the new font, unless there are problems@>;
  1048.     good_font:
  1049.     @<Final checks; various mild errors which often are symptoms of bugs@>
  1050.     incr(nf) ; {the new font is officially present}
  1051.     bad_font: if font_ok then close_binary(tfm_file);
  1052.   end;
  1053. @ First we read the parameters from the \.{DVI} file. Whatever errors are
  1054. found, we must try to do this, or we lose place in the file.
  1055. @<|font_def| vars@>=
  1056.   scale_size, design_size, k, f : integer;
  1057.   dir_len,      {length of the area/directory spec}
  1058.   nam_len:byte; {length of the font name proper}
  1059.   font_mag: real_num;
  1060. @ @<Read the font parameters...@>=
  1061.   @!D_check := get_integer(dvi)(-4) ;
  1062.   scale_size:= get_integer(dvi)( -4) ;
  1063.   design_size:= get_integer(dvi)(-4) ;
  1064.   dir_len:= get_integer(dvi)(1) ;
  1065.   nam_len:= get_integer(dvi)(1) ;
  1066.   nam_len := nam_len + dir_len ;
  1067.   if nam_len = 0 then
  1068.   font_error('null font name! ')
  1069.     @.error: null font name@>
  1070.   else if nam_len >= string_length then
  1071.   font_error('too-long font name! length =  ', nam_len:1 ) ;
  1072.     @.error: too-long font name@>
  1073.   tfm_name := blank ;
  1074.   for k:=1 to nam_len do begin
  1075.     tfm_name.data[k] := zchr(get_byte(dvi)) ;
  1076.   end;
  1077.   tfm_name.len := nam_len ;
  1078.   upcase(tfm_name) ;
  1079.   {AKT: removed
  1080.   display_ln(' ');
  1081.   string_show(tfm_name);
  1082.   display(' ');
  1083. @ Next, check that the sizes are reasonable:
  1084. @<Read the font parameters...@>=
  1085.   if (scale_size<=0)or(scale_size>=@'1000000000) then
  1086.   font_error('--- bad scale (',scale_size:1,')!')
  1087.     @.error: bad scale@>
  1088.   else if (design_size<=0)or(design_size>=@'1000000000) then
  1089.   font_error('--- bad design size (',design_size:1,')!') ;
  1090.     @.error: bad design size@>
  1091.   font_mag := scale_size/design_size ;
  1092.   if (font_mag > 1000) or (font_mag < 0.001) then
  1093.   warn('thats a very unusual font magnification!!! ', font_mag) ;
  1094.     @.error: unusual font mag...@>
  1095.   if nf=max_D_fonts then
  1096.   abort('Crudetype capacity exceeded (max fonts=', max_D_fonts:1,')!');
  1097.     @.Fatal: Capacity exceeded... @>
  1098.   font_num[nf]:=D_new; f:=0;
  1099.   while font_num[f]<>D_new do incr(f);
  1100.   if f<nf then font_error('---this font was already defined!');
  1101.     @.error: font already defined@>
  1102.   font_space[nf] := scale_size div 6 ; {a `thin space' }
  1103. @* Loading the font file.
  1104. See \.{TFTOPL} or \TeX 82 for details of the \.{TFM} file format. The
  1105. description given in \.{TUGboat} (Vol.2, no. 1) is apparently no longer
  1106. accurate. The only difference that I have seen is that all words of the font
  1107. header array after the first 2 are now apparently regarded as optional.
  1108. @.TFTOPL@> @.TeX82@> @.TUGboat@>
  1109. @<Try to load...@>=
  1110.   @<Open font file@>
  1111.   if not font_ok then
  1112.   font_error('---TFM file can''t be opened!');
  1113.     @.error: TFM file cant be opened@>
  1114.   @<Read past the header data, leave the file pointer just after the header@>
  1115.   @<Read the character-width indices@>
  1116.   @<Read the widths, copy them into the font array@>
  1117. @ @<|font_def| vars@>=
  1118.   @!TFM_check,
  1119.   @!lh, {length of the header data, in four-byte words}
  1120.   @!nw:integer; {number of words in the width table}
  1121. @ @<Read past the header...@>=
  1122.   skip(tfm)(2);                     lh:= get_integer(tfm)(2);
  1123.   first_ch[nf]:=get_integer(tfm)(2);   last_ch[nf]:=get_integer(tfm)(2);
  1124.   if (last_ch[nf]<first_ch[nf]) or (last_ch[nf] > max_D_char) then
  1125.   font_error(
  1126.     'Illegal values for first_char and/or last_char, first_char = ',
  1127.       first_ch[nf]:1 , ' last_char = ', last_ch[nf]:1 );
  1128.       @.error: Illegal value@>
  1129.   nw:=get_integer(tfm)(2);
  1130.   if (nw=0)or(nw>256) then
  1131.   font_error('Illegal value for nw, nw= ', nw );
  1132.     @.error: Illegal value@>
  1133.   skip(tfm)(14);
  1134.   TFM_check := get_integer(tfm)(-4);
  1135.   skip(tfm)(4);
  1136.   @<Get coding scheme and re-align file, then see if the printer knows it@>
  1137. @ The header contains |4*lh| bytes, of which 8 have been read so far. If it
  1138. conforms to the \.{TUGboat} format, then the next byte (|@!ck|, say) is the
  1139. number of bytes in the coding scheme name. So, first we must try to see if a
  1140. scheme is present; if so, then we will read |ck+1| bytes and chuck the rest.
  1141. If no coding scheme is present, we simply skip the rest of the header.
  1142. Internally, scheme names are represented by |var_string|s.
  1143. @<Get cod...@>=
  1144.   TFM_scheme := blank ;
  1145.   if lh < 2 then font_error( ' Header must have at least 2 words')
  1146.   else if lh = 2 then do_nothing
  1147.   else begin
  1148.     ck := get_byte(tfm);
  1149.     if ( ck >= 40 ) or ( ck > 4*lh - 9) then
  1150.     skip(tfm)(4*lh - 9)
  1151.     {there is something here, but not a coding scheme}
  1152.     else begin
  1153.       TFM_scheme.len := ck ;
  1154.       for j := 1 to ck do
  1155.       TFM_scheme.data[j] := zchr(get_byte(tfm)) ;
  1156.       skip(tfm)(4*lh - ck - 9);
  1157.       upcase(TFM_scheme) ;
  1158.     end;
  1159.   end;
  1160. @ @<|font_def| vars@>=
  1161.   j , ck : byte ;
  1162.   @!coding_scheme, TFM_scheme: var_string ; {coding scheme of current font}
  1163. @ Now we can start reading the character widths.
  1164. @<|font_def| vars@>=
  1165.   @!in_width:array[byte] of integer; {\.{TFM} width data in \.{DVI} units}
  1166.   @!wid_ptr: array[byte] of byte ; {pointers into |in_width|}
  1167.   b3,b2,b1,b0: byte;       {bytes from \.{TFM} file}
  1168.   @!alpha,@!beta, @!z :integer;
  1169. @ @< Read the character-width indices...@>=
  1170.   for k:=first_ch[nf] to last_ch[nf] do
  1171.   begin wid_ptr[k] := get_byte(tfm); skip(tfm)(3);
  1172.     if wid_ptr[k] > nw then font_error('impossible width ' , wid_ptr[k]);
  1173.   end;
  1174.     @.error: impossible width @>
  1175. @ Here is the width computation. This code is copied from \.{DVItype}. See
  1176. that program for an explanation.
  1177. @<Read the font parameters...@>=
  1178.   z := scale_size ;
  1179.   alpha:=16*z; beta:=16;
  1180.   while z>=@'40000000 do
  1181.   begin z:=z div 2; beta:=beta div 2;
  1182.   end;
  1183. @ @<Read the widths...@>=
  1184.   for k:=0 to nw-1 do
  1185.   begin
  1186.     b0 := get_byte(tfm); b1 := get_byte(tfm);
  1187.     b2 := get_byte(tfm); b3 := get_byte(tfm);
  1188.     in_width[k]:=
  1189.     (((((b3*z)div@'400)+(b2*z))div@'400)+(b1*z))div beta;
  1190.     if  b0 = 255 then in_width[k]:=in_width[k]-alpha
  1191.     else if b0 <> 0 then
  1192.     font_error('Out-of-bounds value for b0') ;
  1193.       @.error: font: Out-of-bounds |b0|@>
  1194.   end ;
  1195. @ Rounding widths. This bit of \.{DVItype} is changed, because \.{Crudetype}
  1196. has to calculate rounded positions by a completely different method.
  1197. @<Read the widths...@>=
  1198.   if in_width[0]<>0 then font_error('the first width should be zero ');
  1199.     @.error: first width...@>
  1200.   for k:= first_ch[nf] to last_ch[nf] do
  1201.   D_width[nf, k] := in_width[ wid_ptr[k]] ;
  1202. @ Then there are various erroneous states that do not necessarily show that
  1203. the font is corrupt, but may indicate bugs in the program. In principle, a
  1204. character might have negative width, but I do not believe it.
  1205. @d bad_char = -32766  {Indicates an unprintable character}
  1206. @d foot == 50000000    {about a foot}
  1207. @<Final checks...@>=
  1208.   for k:= first_ch[nf] to last_ch[nf] do
  1209.   if (D_width[nf, k] < 0) or (D_width[nf, k] > foot) then begin
  1210.     warn('Way-out width = ', D_width[nf,k]:1,
  1211.       'DVI units, character number ', k:1 );
  1212.     codes[ scheme[nf], k].breadth := bad_char ;
  1213.   end;
  1214.   if (D_check<>0)and(TFM_check<>0)and(D_check<>TFM_check) then
  1215.   begin warn('check sums do not agree!');
  1216.       @.error: check sums...@>
  1217.     display_ln('DVI check was: ', D_check, ' TFM check was: ', TFM_check);
  1218.     display('   ');
  1219.   end;
  1220.   {AKT: removed display_ln('---loaded at size ',scale_size:1,' DVI units');}
  1221.   font_mag := (font_mag -1) * 100.0 ;
  1222.   {AKT: removed
  1223.   if abs(font_mag) > 1 then
  1224.   begin display_ln(' ');
  1225.     display_ln(' (this font is magnified ', round(font_mag):1,'%)');
  1226.   end;
  1227.     @.this font is magnified@>@.error: Way-out width@>
  1228. @* Coding schemes.
  1229. In this section we describe the mapping from characters in \TeX\ fonts to
  1230. characters in the printer's fonts (which are presumably much fewer). All
  1231. characters on a crude printer are the same size. We therefore need one piece
  1232. of data, not for each \TeX\ font, but for each coding scheme. The mapping is
  1233. defined in an array called |codes|. For each character |c| in a \TeX\ font
  1234. whose coding scheme has internal number |s|, |codes[s,c]| describes the
  1235. corresponding printer character. Also |known_schemes[s]| is a character
  1236. string which usually contains the coding scheme of that \TeX\ font.
  1237. |max_codes| is the number of coding schemes the program knows about. First,
  1238. define that structure:
  1239. @ @<Glob...@>=
  1240.   @!known_schemes: array[code_ptr] of var_string ;
  1241.   @!codes: array[code_ptr, D_char_ptr] of code_object;
  1242.   no_char: code_object ;
  1243. @ @<Types...@>=
  1244.   code_object = packed record
  1245.     breadth: i_word ;
  1246.     case boolean of
  1247.       true: (IM_font: byte ; IM_char: byte );
  1248.         {Printers font and character}
  1249.       false: (multi: i_word) ;
  1250.   end;
  1251.   @! code_ptr = 0..max_codes;
  1252.     {0 is a coding scheme the printer doesnt know about}
  1253. @ Initially, all these tables are blank. If |c| is a |code_object|, then
  1254. |c.breadth| will usually be its printed width in |h_steps|. |c.breadth =
  1255. bad_char| indicates that the character is unprintable. |bad_char| can be any
  1256. large negative value. Other negative values of |@!breadth| indicate other
  1257. types of peculiar characters.
  1258. @d down_loaded = -32765
  1259. @<Set init...@>=
  1260.   no_char.breadth := bad_char ;
  1261.   no_char.IM_font := 0 ;
  1262.   no_char.IM_char := 0 ;
  1263.   for in_i := 0 to max_codes do
  1264.   for in_j := 0 to max_D_char do begin
  1265.     codes[in_i, in_j] := no_char ;
  1266.   end;
  1267. @ So when a font is read in, we try to assign the right value to its |scheme|.
  1268. If the printer is not absolutely crude, then it might have italic or bold
  1269. fonts. Then we might want a coding scheme to correspond to a single \TeX\
  1270. font. So first we look at the actual font name and see if that matches any
  1271. of the |known_schemes|. But if the printer is |fixed_width|, then all fonts
  1272. of the same face are the same size, so we drop the font size digits off the
  1273. end of the name.
  1274. @<Get cod...@>=
  1275.   k := tfm_name.len ;
  1276.   if fixed_width then
  1277.   while (zord(tfm_name.data[k]) >= "0" ) and
  1278.     (zord(tfm_name.data[k]) <= "9" ) do
  1279.   decr(k) ;
  1280.   coding_scheme:= tfm_name;
  1281.   chop_length(coding_scheme, k) ;
  1282.   j := max_codes ;
  1283.   while (j > 0) and (coding_scheme.data <> known_schemes[j].data ) do decr(j);
  1284.   scheme[nf] := j ;
  1285.   if j = 0 then
  1286. @ If the font name is not in |known_schemes|, then we try again with the
  1287. scheme given in the \.{TFM} file. If that fails, then try if we can download
  1288. the font. If that fails, then the font is deemed to be unprintable, so we do
  1289. not load it.
  1290. @<Get cod...@>=
  1291.   begin
  1292.     j := max_codes ;
  1293.     while (j > 0) and (TFM_scheme.data <> known_schemes[j].data ) do decr(j);
  1294.     scheme[nf] := j ;
  1295.   end;
  1296.   if (j = 0) and can_dl_font then
  1297.   @<Download a whole font@>
  1298.   else if j = 0 then begin
  1299.     scheme[nf] := 9 ;               {AKT: handle PostScript font }
  1300.     {AKT: was
  1301.     display ('Scheme is: ') ;
  1302.     string_show(TFM_scheme) ;
  1303.     font_error(' That coding scheme is unknown' );
  1304.     AKT}
  1305.   end;
  1306.     @.error: unknown coding scheme @>
  1307. @ This procedure sets a character. The character to be set is number |@!c_num|
  1308. in the current font. I have deleted the bit of \.{DVItype} that deals with
  1309. oriental fonts, as I dont believe that crude printers can support them.
  1310. @<Medium...@>=
  1311.   procedure set_character(c_num: integer );
  1312.   var cod: code_object;
  1313.   d_i, d_j : integer; {Used for downloading}
  1314.   begin
  1315.     if cur_scheme = 0 then
  1316.     else if (c_num < first_ch[D_font] ) or (c_num > last_ch[D_font] )
  1317.     then begin
  1318.       warn('character ',c_num:1,' invalid in font number ',
  1319.         font_num[ D_font]:1 );
  1320.         @.error: character invalid...@>
  1321.     end
  1322.     else begin
  1323.       cod := codes[ cur_scheme, c_num];
  1324.       if cod.breadth <> bad_char then begin
  1325.         round_IM_h( c_num) ;
  1326.         if cod.breadth = down_loaded then
  1327.         @<Enter a download request for |cod| and adjust its |breadth|@> ;
  1328.         do_set_char(IM_v, IM_h, cod ) ;
  1329.         @<Do messy things to adjust the positions |D_h|, |IM_h|, etc@>;
  1330.       end;
  1331.     end;
  1332.   end;
  1333. @ @<Forw...@>=
  1334.   procedure do_set_char(Set_v, Set_h: i_word; cod: code_object ); forward;
  1335. @ @<Lowest...@>=
  1336.   procedure do_set_char ;
  1337.   var k_i, k_k, temp_v, temp_h: i_word ;
  1338.   m_c: code_object ;
  1339.   k_ptr: 1..max_ligs;
  1340.   begin
  1341.     if cod.breadth >= 0 then begin
  1342.       @<Check the position@>
  1343.       @<Add the record to the page image@>
  1344.     end
  1345.     else if cod.breadth = bad_char then do_nothing
  1346.     else @<Set multi-character command@> ;
  1347.   end;
  1348. @* Multiple-character commands.
  1349. Several crude printers (e.g. daisy-wheels) have only a limited set of
  1350. characters, which cannot be extended. Sometimes you can generate more
  1351. characters by overstriking. \.{Crudetype} can be programmed to do this, by
  1352. placing suitable entries into a table called |ligatures|. The name is chosen
  1353. by analogy with the |lig_kern| programs in \.{TFM} files, but the data is
  1354. completely different. When one \TeX\ character maps onto several printer
  1355. characters, we call the image a `multi-character' command.
  1356. @<Const...@>=
  1357.   max_ligs = 10000 ;
  1358. @ @<Glob...@>=
  1359.   ligatures : array[1..max_ligs] of lig_thing;
  1360.   top_of_ligs: 0..max_ligs ; {highest used point in |ligatures|}
  1361. @ @<Types...@>=
  1362.   trio = 1..3 ;
  1363.   lig_thing = packed record
  1364.     case trio of
  1365.       1: (v_move: i_word ;
  1366.         h_move: i_word) ;
  1367.       2: (code: code_object) ;
  1368.       3: (num : i_word ;
  1369.         guard : i_word) ;
  1370.   end;
  1371. @ @<Set init...@>=
  1372.   top_of_ligs := 0;
  1373.   for in_i := 1 to max_ligs do ligatures[ in_i].code := no_char ;
  1374. @ The |code_object| addresses a multiple character when its |breadth| is
  1375. negative, and not one of the special classes defined above. It must then be
  1376. the |false| variant, and its |multi| field (which must be |>0|) points to the
  1377. corresponding entry in |ligatures|. Suppose that field is |c| . Then
  1378. |ligatures[c]| is the last entry of a string of items that defines the
  1379. replacement text of the |code|. It should be of the third variant; The |num|
  1380. field of this entry counts the number of characters that |code| expands into.
  1381. The |guard| field is an arbitrary impossible value called |sentry| to give a
  1382. check on the data in |ligatures| .
  1383. @d sentry = -32767
  1384. @<Set multi...@>=
  1385.   begin
  1386.     if (cod.multi <= 0) or (cod.multi > top_of_ligs) then
  1387.     warn('Illegal value of char in multi-character command')
  1388.       @.error: Illegal value@>
  1389.     else begin
  1390.       k_ptr := cod.multi ;
  1391.       if ligatures[k_ptr].guard <> sentry then
  1392.       warn('Sentry not found in Kerns ' ) ;
  1393.         @.error: Sentry ...@>
  1394.       k_i := ligatures[k_ptr].num ;
  1395.       k_ptr := k_ptr - 2*k_i ;
  1396.       if (k_i <= 0) or (k_ptr < 0 ) then
  1397.       warn('Illegal value of k_i in multi_character command');
  1398.         @.error: Illegal value@>
  1399.       for  k_k := 1 to k_i do
  1400.       @<Get that character and write it @>;
  1401.     end;
  1402.   end
  1403. @ Each character of a multi-character command needs 2 entries in |ligatures|.
  1404. The first defines the position, the second defines the character. |v_move| and
  1405. |h_move| are relative to the current (rounded) position |Set_v, Set_h| and use
  1406. the same units. A multi-character command can call another one recursively.
  1407. @<Get that character ...@>=
  1408.   begin
  1409.     temp_v := Set_v + ligatures[k_ptr].v_move ;
  1410.     temp_h := Set_h + ligatures[k_ptr].h_move ;
  1411.     incr(k_ptr);
  1412.     m_c := ligatures[k_ptr].code ;
  1413.     do_set_char(temp_v, temp_h, m_c ) ;
  1414.     incr(k_ptr);
  1415.   end;
  1416. @* Getting data into the |codes| array.
  1417. This is clearly a very long and error-prone job, so the next procedures are put
  1418. in to reduce this. First suppose that: in the \TeX\ coding scheme with number
  1419. |s|, a run of |length| characters starting from |start| maps onto a run of
  1420. consecutive characters in printer font |PR_font|, starting at |PR_first|. This
  1421. procedure will enter the whole run at one go.
  1422. @<Lowest...@>=
  1423.   procedure alphabet
  1424.   (start, length: byte; s: code_ptr ; PR_font, PR_first : byte );
  1425.   var i:integer; ccc:code_object;
  1426.   begin @<Check alphabet data@>;
  1427.     ccc.IM_font := PR_font ;
  1428.     ccc.breadth := char_width ;
  1429.     for i := 0 to length-1 do begin
  1430.       ccc.IM_char := PR_first +i;
  1431.       codes[s, start+i] := ccc ;
  1432.     end; end;
  1433. @ @<Check alph...@>=
  1434.   if (s < 1) then abort('alphabet: scheme < 1 ')
  1435.   else if (s > max_codes) then abort('alphabet: scheme too large')
  1436.   else if (PR_first < 0) then abort('alphabet: negative first')
  1437.   else if (start < 0) then abort('alphabet:  negative start')
  1438.   else if (length < 0) then abort('alphabet: negative length')
  1439.   else if (start + length -1 > max_D_char) then abort('alphabet: overflow')
  1440.     @.Fatal: alphabet...@>
  1441. @ Clearly, |alphabet| will only cover a very small part of the problem.  The
  1442. next procedure enters data into a subset of the |codes| array corresponding to
  1443. a single row of a \TeX\ font. In the standard font tables, row number |m| is
  1444. the subrange |8*m..8*m+7| of a font. It is hoped that when the calls of
  1445. procedure |row| are written out in a program, the result will be (just about)
  1446. legible, whereas a flood of statements like  \begintt
  1447.          codes[i,j].IM_font := 121; \endtt
  1448. is certainly not legible.
  1449. The parameters are as follows. |@!row_spec| specifies what characters are to
  1450. go into the row. |@!scheme | is the number assigned to the \TeX\ coding scheme
  1451. within the program. |@!row_num | is the number of the row in that scheme
  1452. (starting from 0). |@!first_font|  is the initial printer font.
  1453. @<Top...@>=
  1454.   procedure row
  1455.     (row_spec: row_str; scheme, row_num: integer; first_font: i_word );
  1456.   var n :integer;  codd: code_object;
  1457.   begin
  1458.     incr(row_count);
  1459.     row_pt := 1 ;
  1460.     row_font := first_font ;
  1461.     row_string := row_spec;
  1462.     for n := 8*row_num to  8*row_num + 7 do begin
  1463.       row_char (0 ,codd);
  1464.       if codd.breadth = bad_char then do_nothing
  1465.       else codes[ scheme, n ] := codd ;
  1466.     end;
  1467.   end;
  1468. @ \.{TANGLE} imposes a limit of 69 on the length of quoted strings. This is a
  1469. considerable nuisance, as we could make the |row_spec| strings look much better
  1470. if they could be longer.
  1471. @<Const...@>=
  1472.   row_length = 69 ;
  1473. @ @<Types...@>=
  1474.   row_str = packed array [1..row_length] of char ;
  1475. @ @<Glob...@>=
  1476.   row_pt: integer;  {Points to next char from |row_spec|}
  1477.   row_font: i_word; {printer font being addressed during the |row| procedure}
  1478.   row_string: row_str ;
  1479.   row_count: integer ;
  1480. @ @<Set init...@>=
  1481.   row_count:= 0 ;
  1482. @ In order to help debugging, error messages will print |row_string| and a
  1483. pointer. The diagnostics of |row| are known to be very poor; I have not
  1484. bothered to fix them because up to now they have been adequate, and they are
  1485. really meant for the installer rather than the end user.
  1486. @d row_warn(#) == begin
  1487.   display_ln(row_string ) ;
  1488.   display_ln('^' : row_pt-1 ) ;
  1489.   warn('Row: ', #);
  1490.   return;
  1491. @ The overall format of the |row_spec| is a set of 8 character specifiers
  1492. separated by one or more spaces. The procedure |row_char| reads one character
  1493. specifier from the |row_string|, and constructs the specified |code_object|.
  1494. Logically, |row_char| should be a function and return that |code_object| as
  1495. its value. \PASCAL\ does not permit this. So we assemble the result in the
  1496. variable parameter |value|.
  1497. @<Medium...@>=
  1498.   procedure row_char(context: integer; var value: code_object);
  1499.   label exit ;
  1500.   const @<|Row_char| constants@>
  1501.   var c :byte; @<Row locals@>
  1502.   begin
  1503.     value.breadth := char_width ; {default}
  1504.     value.IM_font := row_font ;    {default font}
  1505.     c := row_get ;
  1506.     if ( context = 0) and ( c <> " " ) and ( c <> "Z") then
  1507.       row_warn('Character specifiers must start with at least one space') ;
  1508.     while ( c = " " ) do c := row_get ;
  1509.     @<Escape sequences in the |row_spec| @>
  1510.     else value.IM_char := c ;
  1511.   exit: end;
  1512.     @.error: Row: Character spec...@>
  1513. @ There are several escape sequences that need to go into the |rowstring|.
  1514. Since all the PLAIN.TEX coding schemes (except the math extension one) have
  1515. the upper case Roman characters in their ASCII positions, these characters
  1516. will surely be inserted into |codes| by the |alphabet| procedure. So they are
  1517. available as flag characters. But the brackets are also used as flags, as they
  1518. are so much more perspicuous than anything else. Here is a list of the
  1519. characters currently used as escapes: \begintt
  1520. A C D E F K L M N S Q U W Z \endtt
  1521. This list should be updated if other escapes are added .
  1522. @.Escape sequences@>@.ASCII@>
  1523. @ Some characters, called `bad', have most undesirable effects when used in
  1524. \.{WEB} strings. So the following upper-case letters stand for them. The
  1525. actual characters may not be used, so they generate errors.
  1526. @<Esc...@>=
  1527.   if c = "A" then value.IM_char := 64   {at sign}
  1528.   else if c = "S" then value.IM_char := 32   {a space}
  1529.   else if c = "Q" then value.IM_char := 39   { a single quote char}
  1530.   else if c = "W" then value.IM_char := 34   { a double quote char }
  1531.   else if c = "E" then value.IM_char := 127  { a delete char }
  1532.   else if (c = "'") or (c = """") or (c = "@@") or (c = 127)
  1533.     then row_warn( 'Bad character---Rejected' )
  1534.   else if (c = " ")
  1535.     then row_warn('space found out of context')
  1536.       @.Error: Row: Bad character@>@.error: Row: space found...@>
  1537. @ Then the  `Z' escape is provided to generate a do-nothing  code. This would
  1538. be used if a previous call (say, of |alphabet|) had left a row partly
  1539. incorrect. Then you might issue a call of |row| to change that row. Typing `Z'
  1540. at the positions occupied by correct characters would leave them alone.
  1541. @<Esc...@>=
  1542.   else if c = "Z" then value.breadth :=  bad_char
  1543. @ Since many letters and brackets are used as escapes, the `L' escape is
  1544. needed to enable them to be used Literally. `LL' generates `L'.
  1545. @<Esc...@>=
  1546.   else if c = "L" then value.IM_char := row_get
  1547. @ In order to address printer characters in the range 0..32, where ASCII has
  1548. no graphics, here is a Control escape. This simply reads the next character
  1549. from the |row_spec| and reduces it modulo 32. It is best to use the lower case
  1550. alphabet (the range 95..126) as this avoids all the `bad' characters (and
  1551. their escapes). So control-A should be typed `Ca' , not `CA' .
  1552. Then the Meta escape addresses meta-characters, i.e. those in the range
  1553. 128..255. We cannot just read a character and add 128, as we might want to
  1554. Mutate the ASCII controls, or the `bad' characters. So `M' must read a complete
  1555. |code_object| (respecting the escapes given above) and add 128 to its |IM_char|
  1556. field. So we must say `MS' for `meta-space' = 160 , and  `MLS' for `meta-S'
  1557. = 211 .
  1558. @.ASCII@>
  1559. @d M_con == 250
  1560.     {Context while reading a Meta character}
  1561. @<Esc...@>=
  1562.   else if c = "C" then
  1563.   value.IM_char := row_get mod 32
  1564.   else if context >= M_con then value.IM_char := c
  1565.     {During a Meta, forbid any of the later escapes}
  1566.   else if c = "M" then
  1567.   begin
  1568.     row_char(M_con , value ) ;
  1569.     value.IM_char := value.IM_char + 128 ;
  1570.   end
  1571. @ A |narrow| character is one with zero width. To generate one, precede it
  1572. with an `N' . To mark a character  as down-loadable, precede it with  `D'. A
  1573. character cannot be both narrow and down-loadable.
  1574. @d N_con == 230
  1575.     {Context while reading a Narrow or |down_loaded| character}
  1576. @<Esc...@>=
  1577.   else if ((c = "N" ) or (c = "D" )) and (context >= N_con) then
  1578.   row_warn('Narrow or Down escape out of context')
  1579.     @.error: Row: Narrow escape...@>
  1580.   else if c = "N" then
  1581.   begin
  1582.     row_char(N_con, value ) ;
  1583.     value.breadth := 0 ;
  1584.   end
  1585.   else if c = "D" then
  1586.   begin
  1587.     row_char(N_con, value ) ;
  1588.     value.breadth := down_loaded ;
  1589.   end
  1590. @ Changing printer fonts in the middle of a |row| is done by inserting an `F'
  1591. character, followed by an integer. This is the printer font to be used, from
  1592. now on till the next `F' . Note that the initial font was passed as the 3rd
  1593. parameter to |row|.
  1594. @<Esc...@>=
  1595.   else if c = "F" then begin
  1596.     row_font := row_integer ;
  1597.     if context = 0 then row_char(1, value)
  1598.     else  row_char(context, value);
  1599.   end
  1600. @* Assembling a multi-character in |row|.
  1601. Now we come to the difficult part, which is assembling a multiple-character
  1602. command into the |ligatures| array. For this purpose, we use brackets. Curly
  1603. brackets mean that the characters inside are to be overstruck, square brackets
  1604. mean they are to be typed horizontally, and angle brackets mean that they are
  1605. to be typed vertically above each other. Finally the `U' escape (which must
  1606. come immediately after a |<| ) means to raise the (logical) cursor before
  1607. starting the vertical list.
  1608.     Warning!! I use the numerical (\.{ASCII}) values of these chars
  1609. @.System dependencies@>@.ASCII@>
  1610. @<|Row_char| const...@>=
  1611.   o_bra = "{" ;   o_ket = "}" ;
  1612.   h_bra = "[" ;   h_ket = "]" ;
  1613.   v_bra = "<" ;   v_ket = ">" ;
  1614.     {`o' means overstrike, `h' means horizontal, and `v' vertical}
  1615. @ So if we want to generate a Macsyma style summation sign, which looks like
  1616. this: \begintt
  1617. .                   ====
  1618. .                   \
  1619. .                    >
  1620. .                   /
  1621. .                   ====
  1622. \endtt
  1623. we have to insert the following mess into the |row_spec| string: \begintt
  1624.             <S[====]\[SL>]/[====]>
  1625. \endtt
  1626. The `S' is needed to get correct vertical alignment. The  `L'  is needed to
  1627. prevent the following |>| being taken as a |ket|. See the lineprinter change
  1628. file for examples.
  1629. @ In order to keep some control over all these escape sequences, I have made a
  1630. special rule of syntax. The escape sequences in |row_char| may only be nested
  1631. in a definite order. That order is: (bad characters or Control or Literal)
  1632. inside Meta inside (Narrow or Down-loadable) inside Font inside over-lists
  1633. inside |h_list|s inside |v_list|s. The parameter |context| keeps track of
  1634. this. The innermost constructions have the highest values of |context|. If
  1635. these rules are broken the user should get an error message saying `Row' and
  1636. some diagnostics.
  1637. @<Esc...@>=
  1638.   else if (c = o_bra) or (c = h_bra) or (c = v_bra)
  1639.   then begin
  1640.     if context >=  c then
  1641.     row_warn('Illegal nesting of brackets in row_spec');
  1642.       @.error: Row: Illegal nesting @>
  1643.     @<Assemble characters into |lig_buff| until we read the matching |ket|@>;
  1644.     @<Copy |lig_buff| into |ligatures| and return a pointer to it@>;
  1645.   end
  1646. @ |hoister| and |ender| are arbitrarily selected impossible classes for a
  1647. character, indicating respectively that a |v_list| has to be raised one
  1648. |char_ht| or that a |ket| has been read.
  1649. @d hoister = -32764
  1650. @d ender   = -32763
  1651. @<Assemble...@>=
  1652.   for i := 1 to max_buf do lig_buff[ i].code := no_char ;
  1653.   buf_len := 0; delta_h := 0; delta_v := 0;
  1654.   repeat
  1655.     row_char(c ,row_cod ) ;
  1656.     @<Do suitable action if |row_cod| is peculiar@>
  1657.     else begin
  1658.       incr(buf_len);
  1659.       lig_buff[buf_len].v_move := delta_v ;
  1660.       lig_buff[buf_len].h_move := delta_h ;
  1661.       incr(buf_len);
  1662.       lig_buff[buf_len].code := row_cod ;
  1663.       if c = v_bra then delta_v := delta_v + char_ht;
  1664.       if c = h_bra then delta_h := delta_h + char_width ;
  1665.     end;
  1666.   until row_cod.breadth = ender;
  1667. @ @<Row loc...@>=
  1668.   lig_buff: array[1..max_buf] of lig_thing ;
  1669.   buf_num: 0..max_buf ;
  1670.       {Number of characters (or multi-characters) in current list}
  1671.   buf_len: 0..max_buf ;
  1672.       {Number of used locations in |lig_buff|: should be |2*buf_num|}
  1673.   delta_h, delta_v: i_word;
  1674.   i : integer;
  1675.   row_cod: code_object ;
  1676. @ @<Const...@>=
  1677.   max_buf = 201;
  1678. @ @<Do suitable action...@>=
  1679.   if row_cod.breadth = hoister then delta_v := delta_v - char_ht
  1680.   else if row_cod.breadth = ender then
  1681.   else if buf_len + 3 > max_buf then
  1682.     abort('overflowed lig_buff array')
  1683.     @.Fatal: overflowed |lig_buff|@>
  1684. @ @<Esc...@>=
  1685.   else if (c = "U" ) and (context = v_bra) then value.breadth := hoister
  1686.   else if (c = "U" ) then
  1687.   row_warn('U escape out of context')
  1688.     @.error: Row: U escape...@>
  1689.   else if ((c = o_ket) or (c = h_ket) or (c = v_ket)) and (context = c-2)
  1690.   then value.breadth := ender
  1691.   else if (c = o_ket) or (c = h_ket) or (c = v_ket)
  1692.   then row_warn('mismatching brackets ')
  1693.     @.error: Row: mismatching brackets@>
  1694. @ Yet another escape is the |kern| escape. If the printer has reasonable
  1695. positioning resolution, we may want to move the parts of a multi-character
  1696. about to make them fit together better. So a |kern| takes an integer parameter
  1697. and moves the next component of the current list by that many |steps| against
  1698. the current direction. The reason for going back is that one can easily move
  1699. forwards by setting a blank space.
  1700. @d h_kern = -32762
  1701. @d v_kern = -32761
  1702. @<Esc...@>=
  1703.   else if (c = "K") and (context = h_bra) then value.breadth := h_kern
  1704.   else if (c = "K") and (context = v_bra) then value.breadth := v_kern
  1705.   else if (c = "K")
  1706.   then row_warn('Kern escape out of context' )
  1707.     @.error: Row: Kern escape @>
  1708. @ @<Do suitable action...@>=
  1709.   else if  row_cod.breadth = h_kern then delta_h := delta_h - row_integer
  1710.   else if  row_cod.breadth = v_kern then delta_v := delta_v - row_integer
  1711. @ @<Copy...@>=
  1712.   buf_num := 0 ;
  1713.   if buf_len = 0 then value.breadth :=  bad_char
  1714.   else if top_of_ligs + buf_len + 1 >= max_ligs then
  1715.   abort ('ligature array overflowed, must recompile with larger array')
  1716.     @.Fatal: ligature overflowed@>
  1717.   else begin
  1718.     for i := 1 to buf_len do
  1719.     ligatures[ top_of_ligs + i ] := lig_buff[i] ;
  1720.     top_of_ligs := top_of_ligs + buf_len + 1 ;
  1721.     buf_num := buf_len div 2 ;
  1722.     ligatures[top_of_ligs].num:= buf_num ;
  1723.     ligatures[top_of_ligs].guard := sentry ;
  1724.     value.multi := top_of_ligs ;
  1725.     value.breadth := -20000 ;
  1726.     {Provisional: a nonsense value to make sure the correct value does get
  1727.       inserted later}
  1728.   end;
  1729. @ Finally, here are the two functions that actually read the |row_spec|. The
  1730. first is |row_integer|. This reads an integer parameter for the |font| and
  1731. |kern| escapes. The parameter may have a minus sign and is terminated by the
  1732. next non-digit. (If there needs to be another digit immediately after the
  1733. parameter, then prefix it with an `L').
  1734. @<Lowest...@>=
  1735. function row_integer: integer;
  1736.   label exit ;
  1737.   var neg: boolean ; n: integer ; b: byte ;
  1738.   begin
  1739.     n := 0 ; neg := false ; b := row_get ;
  1740.     if (b = "-") then begin
  1741.       b := row_get ; neg := true;
  1742.     end
  1743.     else if ( b="+") then b := row_get ;
  1744.     if (b < "0" ) or (b > "9" ) then
  1745.     row_warn( 'no digits found by row_integer')
  1746.       @.error: Row: no digits @>
  1747.     else
  1748.     repeat
  1749.       n := n*10 + b - "0" ;
  1750.       b := row_get ;
  1751.     until (b < "0" ) or (b > "9" ) ;
  1752.     if neg then n := -n ;
  1753.     exit: row_integer := n ; decr( row_pt) ;
  1754.   end;
  1755. @ And this function gets the next character from the |row_spec|. I always have
  1756. great difficulty with this sort of program, so will go carefully. Recall:
  1757. |row_pt| points to the next character we are going to read. |b| is that
  1758. character, translated into a byte by |zord|.
  1759. @<Forw...@>=
  1760. function row_get : byte; forward;
  1761. @ @<Lowest...@>=
  1762.   function row_get;
  1763.   label exit ;
  1764.   var b: byte ;
  1765.   begin
  1766.     b:= 127 ; {any bad character}
  1767.     if row_pt > row_length then
  1768.     row_warn('fallen off end of row_string')
  1769.       @.error: Row: fallen off end@>
  1770.     else begin
  1771.       b:= zord(row_string[row_pt]) ;
  1772.       incr(row_pt);
  1773.     end;
  1774.     exit: row_get := b ;
  1775.   end;
  1776. @* Character strings.
  1777. In this section I have tried to provide some tolerable string-handling
  1778. facilities in despite of the restrictions of \PASCAL. This does not seem to
  1779. belong in any particular place in the program, but in view of the horrible
  1780. gruesome things that will happen in the next section, it seemed a good idea to
  1781. give some light relief. That is why this section is inserted here.
  1782. The |var_string| type is principally used for file names and to send command
  1783. sequences to the printer. Logically, these procedures should all be functions
  1784. and return the results, but stupid \PASCAL\ does not allow this. It would of
  1785. course be much cleaner to use the VMS |varying| type, but that would make the
  1786. program non-portable.
  1787. @<Const...@>= string_length = 100 ; {a guess, of course}
  1788. @ @<Types...@>=
  1789.   s_ptr = 1..string_length ;
  1790.   s_dat = packed array[ s_ptr] of char ;
  1791.   var_string= packed record
  1792.   len: byte;
  1793.   data: s_dat ;
  1794.   end ;
  1795. @ |@!blank| is used for initialising strings. It should not be altered
  1796. anywhere but here.
  1797. @<Set init...@>=
  1798.   blank.len := 0 ;
  1799.   for in_i := 1 to string_length do
  1800.   blank.data[in_i] := ' ' ;
  1801. @ @<Glob...@>=
  1802. blank: var_string ;
  1803. @ Here are two small procedures for printing strings.
  1804. @<Lowest...@>=
  1805.   procedure string_show(ss: var_string);
  1806.   var s_n: byte ;
  1807.   begin
  1808.     for s_n := 1 to ss.len do display(ss.data[s_n]) ;
  1809.   end;
  1810.   procedure string_print(ss: var_string);
  1811.   var s_n: byte ;
  1812.   begin
  1813.     for s_n := 1 to ss.len do print(ss.data[s_n]) ;
  1814.   end;
  1815. @ @<Forw...@>=
  1816.   procedure upcase(var s: var_string) ; forward;
  1817.   {convert to upper case}
  1818. @ @<Lowest...@>=
  1819.   procedure upcase ;
  1820.   var i: s_ptr; k: byte ;
  1821.   begin
  1822.     for i := 1 to s.len do begin
  1823.       k:= zord(s.data[i]) ;
  1824.       if (k >= "a" ) and (k <= "z" ) then
  1825.       s.data[i] := zchr(k + "A" - "a" ) ;
  1826.     end;
  1827.   end;
  1828. @ We also use |var_strings| for command strings to be sent to the printer.
  1829. These nearly always use unprintable ASCII characters, typically ESCAPE. So we
  1830. need a special procedure to initialise them. It copies the |in_data| into the
  1831. |result|, but changes each |flag| into  the character |zchr(escape)|. We
  1832. determine the length by assuming that the |in_data| string is padded with some
  1833. character; then we run backwards along it until we hit the latest non-pad
  1834. character. Since \.{TANGLE} imposes a limit of 69 on the length of quoted
  1835. strings, we use the |row_str| type defined elsewhere.
  1836.   @.ASCII@>
  1837. @<Lowest...@>=
  1838.   procedure set_string
  1839.   (var result: var_string; in_data: row_str;
  1840.   flag: char; escape: byte );
  1841.   var i: byte ; last:char ;
  1842.   begin
  1843.     result := blank ;
  1844.     i := row_length ;
  1845.     last := in_data[i] ;
  1846.     while (in_data[i] = last) and (i > 1 ) do decr(i) ;
  1847.     if (i=1) and (in_data[1] = last ) then i := 0 ;
  1848.     {YEUCH! but if I write this in a natural way, it crashes when |i=0|}
  1849.     result.len := i ;
  1850.     for i := 1 to result.len do
  1851.     if in_data[i] = flag then result.data[i] := zchr(escape)
  1852.     else result.data[i] := in_data[i] ;
  1853.   end;
  1854.   procedure add_char(var s: var_string; c: char) ;
  1855.   begin
  1856.     if s.len >= string_length then warn('string too long')
  1857.     else begin
  1858.       incr(s.len) ;
  1859.       s.data[s.len] := c ;
  1860.     end;
  1861.   end;
  1862.     @.error: string too long@>
  1863.   procedure append(var head: var_string; tail: var_string) ;
  1864.   var k: integer;
  1865.   begin
  1866.     if head.len + tail.len > string_length
  1867.     then warn('string too long')
  1868.     else begin
  1869.       for k := 1 to tail.len do
  1870.       head.data[ k + head.len] := tail.data[ k] ;
  1871.       head.len := head.len + tail.len ;
  1872.     end;
  1873.   end;
  1874.     @.error: string too long@>
  1875. @ The next procedures generate substrings. If the character |c| is present in
  1876. |s|, then |chop_top| deletes the first |c| from |s|, and all successive
  1877. characters. |chop_tail| deletes the last |c| and all previous characters.
  1878. |chop_length| chops the string to the stated length.
  1879. @<Lowest...@>=
  1880.   procedure chop_top(var s: var_string; c:char );
  1881.   var t: var_string; i,n: byte ;
  1882.   begin
  1883.     n := 1 ;
  1884.     while (n <= s.len ) and (s.data[n] <> c) do incr(n);
  1885.     if n <= s.len then begin
  1886.       t := blank ;
  1887.       for i := 1 to n-1 do t.data[i] := s.data[i ];
  1888.       t.len := n-1 ;
  1889.       s := t ;
  1890.     end;
  1891.   end;
  1892.   procedure chop_tail(var s: var_string; c:char );
  1893.   var t: var_string; i,n: byte ;
  1894.   begin
  1895.     n := s.len ;
  1896.     while (n >= 1 ) and (s.data[n] <> c) do decr(n);
  1897.     if n >= 1 then begin
  1898.       t := blank ;
  1899.       for i := 1 to s.len - n do t.data[i] := s.data[n+i];
  1900.       t.len := s.len - n;
  1901.       s := t ;
  1902.     end;
  1903.   end;
  1904.   procedure chop_length(var s: var_string; k: integer);
  1905.   var n: integer;
  1906.   begin
  1907.     if (k < 0 ) or (k > s.len) then
  1908.       warn ('Impossible length supplied to chop_length' )
  1909.     else begin
  1910.       for n:= k+1 to s.len do s.data[n] := ' ' ;
  1911.       s.len := k ;
  1912.     end;
  1913.   end;
  1914.     @.error: impossible length@>
  1915. @ Printer commands usually have the format (prefix)(parameter)(suffix). These
  1916. procedures print the parameter. |s| is one character, and may have the values
  1917. `B'(yte), `D'(ecimal), `H'(exadecimal), `O'(ctal), or `W' (a 16-bit signed
  1918. word, in twos-complement notation).
  1919. @<Lowest...@>=
  1920.   procedure string_base(var result:var_string; n:integer; s:byte) ;
  1921.   { |n| to base |s| . Note that the integer is appended to |result|}
  1922.   var nh : integer ;
  1923.   begin
  1924.     nh := n ;
  1925.     if nh < 0 then begin add_char(result, '-'); nh := - nh ; end ;
  1926.     if nh >= s then begin
  1927.       string_base(result, nh div s, s) ;
  1928.       nh := nh mod s ;
  1929.     end ;
  1930.     if nh >= 10 then add_char(result, zchr(nh - 10 + "A" ))
  1931.     else add_char( result, zchr(nh + "0"  )) ;
  1932.   end;
  1933.   procedure string_integer (var ss: var_string; n:integer; c:char);
  1934.   var nn: integer ;
  1935.   begin if c = 'O' then string_base(ss, n, 8)
  1936.     else if c = 'H' then string_base(ss, n, 16)
  1937.     else if c = 'D' then string_base(ss, n, 10 )
  1938.     else if (c = 'B') and (n >= 0) and (n <= 255)
  1939.     then add_char(ss, zchr(n))
  1940.     else if c = 'B' then warn('out-of-range byte')
  1941.     else if c='W' then begin
  1942.       if (n>= 0) and (n <= 32767) then begin
  1943.         add_char(ss, zchr(n div 256));
  1944.         add_char(ss, zchr(n mod 256));
  1945.       end
  1946.       else if (n<0 ) and (n> -32768) then begin
  1947.         nn := n + 65536 ;
  1948.         add_char(ss, zchr(nn div 256));
  1949.         add_char(ss, zchr(nn mod 256));
  1950.       end
  1951.       else warn('out-of-range word') ;
  1952.     end
  1953.     @<Hook for weird parameter types@>
  1954.     else warn('string_integer called with illegal type') ;
  1955.   end;
  1956.     @.error: out-of-range...@> @.error: string_integer called...@>
  1957.   procedure print_integer (n:integer; c:char);
  1958.   var ss: var_string;
  1959.   begin ss := blank ;
  1960.     string_integer(ss, n, c) ;
  1961.     string_print(ss) ;
  1962.   end;
  1963. @ @<Hook for weird ...@>=
  1964. @* Translating the device-independent file, 5: Movements.
  1965. This section considers the problem of deciding where each character has to be
  1966. printed on the printer's page. This is by far and away the most difficult (and
  1967. unsatisfactory) part of \.{Crudetype}. The current version is not a properly
  1968. designed algorithm; it is merely a bodge, obtained by a lot of trial and
  1969. error. It does seem to give tolerable results on \.{WEB} files, lineprinter,
  1970. and VMS. The main variables are: |@!D_h| is `\TeX's cursor'. It gives the
  1971. `exact' horizontal position (in \.{DVI} units) generated by \.{DVI} commands.
  1972. This is always updated exactly as in \.{DVItype}. |@!IM_h| is the `page
  1973. image's cursor'. It marks the position (in |h_steps|) where the next character
  1974. will be set.
  1975. The procedure |round_IM_h| is called immediately before we set a character or
  1976. a rule. We have to take account of all the movements that occurred since the
  1977. last previous character was set.
  1978. @<Forw...@>= procedure round_IM_h( code: byte); forward ;
  1979. @ @<Lowest...@>=
  1980.   procedure round_IM_h ;
  1981.   var
  1982.   s_top, diff, n, m,
  1983.   delta, new_IM_h, rounded_h : integer ;
  1984.   begin
  1985.     @<Find the new position |new_IM_h|@>
  1986.     IM_h := new_IM_h ;
  1987.   end;
  1988. @ The obvious method is to multiply |D_h| by a factor |h_conv| and round to
  1989. nearest integer. This gives extremely bad results, because the characters in
  1990. \TeX\ fonts vary enormously in width, while many crude printers have
  1991. |fixed_width| characters. If |h_conv| is too large, then you get spaces in the
  1992. middle of words. If |h_conv| is too small, then successive characters in a
  1993. word get printed on top of each other. With an intermediate value of |h_conv|,
  1994. you get both effects at once; in other words, the characters in \TeX\ fonts
  1995. vary so much in width that the `too large' and `too small' values of |h_conv|
  1996. overlap. In this situation, a great deal of jiggery-pokery is needed to get a
  1997. tolerable result (sometimes! I have not been able to make this code work in
  1998. general.)
  1999.   For a start, here is the algorithm used in \.{DVItype}. |D_h_right| and
  2000. |IM_h_right| give the latest value of |D_h| and |IM_h| after the latest
  2001. previous character or rule was set. If the horizontal motion is small, like a
  2002. kern, |IM_h| changes by rounding the kern; but when the motion is large,
  2003. |IM_h| changes by rounding the true position |D_h| so that accumulated
  2004. rounding errors disappear. Also, we insist that the total amount of drift is
  2005. bounded.
  2006. @d h_step_round(#) == round(h_conv * # )
  2007. @d max_drift == 2
  2008. @<Find the new position |new_IM_h|@>=
  2009.   rounded_h := h_step_round(D_h) + l_margin ;
  2010.   delta := D_h - D_h_right ;
  2011.   if (delta > thin_space) or (delta <= -4*thin_space) then
  2012.   new_IM_h := rounded_h
  2013.   else new_IM_h := IM_h_right + h_step_round(delta);
  2014.   if not fixed_width then begin
  2015.     if new_IM_h > rounded_h + max_drift
  2016.       then new_IM_h := rounded_h + max_drift
  2017.     else if new_IM_h < rounded_h - max_drift
  2018.       then new_IM_h := rounded_h - max_drift ;
  2019.   end else
  2020. @ Calculating |IM_h|  on a |fixed_width| printer is very hairy. If we are not
  2021. careful, then the spaces between words will sometimes get rounded to 0. Since
  2022. we round `large' movements by rounding |D_h|, the space may even get rounded to
  2023. a negative value, if there was previously a lot of drift. So we must re-round
  2024. |new_IM_h|. The next idea is that whenever \TeX\ moves right by an amount that
  2025. seems large enough to be a space between words, we force |IM_h| to increase.
  2026. @<Find the new position |new_IM_h|@>=
  2027.   if (delta > thin_space) and (new_IM_h < IM_h_right + gap_width)
  2028.     @<Except in some special cases@>
  2029.   then new_IM_h := IM_h_right + gap_width
  2030.   else if (delta > thin_space) then do_nothing
  2031.   else if (delta > - 2*thin_space)
  2032.   then new_IM_h := IM_h_right
  2033.   else
  2034. @ Here are two little fudges which improve the result. First, when \TeX\ puts
  2035. out a thin space, it sometimes is a bit too small to be recognised as such. So
  2036. we reduce the |font_space| when a font is defined.
  2037. @<Read the font parameters...@>=
  2038.   font_space[nf] := round(font_space[nf] * 0.99 ) ;
  2039. @ The next fudge is needed to handle tables of contents. \TeX\ prints these by
  2040. putting out long streams of dots with small spaces in between. If these spaces
  2041. all get expanded to a whole character width, the right hand columns get thrown
  2042. right off the paper. So dont expand if the next character is a stop or comma.
  2043. @<Except in some special cases@>=
  2044.   and not ( ( ( code = ".") or ( code = ",") ) and
  2045.     ( ( cur_scheme > 0)  and ( cur_scheme <= max_plain )))
  2046. @ When these alternatives fail, we have lost contact between |D_h| and
  2047. |D_h_right|. This happens when \TeX\ makes a large backspace; in fact \TeX\
  2048. seems nearly always to do large backspaces by |pop| rather than an explicit
  2049. move left. \TeX\ often expresses boxes by a sequence like this:
  2050. \centerline{\tt{
  2051. PUS\markarrow{H}  Move right ------------>
  2052. \markarrow{[}set characters] \markarrow{P}OP   }}
  2053. followed by zero or more |push|es, then by a move either to one of the
  2054. positions marked by the arrows, or close by. I try to deal with this by
  2055. dropping markers at each of the arrowed positions. The markers are labelled
  2056. |D_h_right|, etc, and each marker has a corresponding value of |IM_h|
  2057. attached.
  2058. @<Glob...@>=
  2059.   D_h_left, IM_h_left, D_h_mid, IM_h_mid, D_h_right, IM_h_right,  {the markers}
  2060.   IM_h, IM_v, D_dis, IM_dis: integer;
  2061.   IM_h_stack, IM_v_stack:
  2062.     array [0..max_stack+2] of integer; {pushed down values }
  2063. @ Suppose that we are about to set a character, and |D_h-D_h_right| is large
  2064. and negative. Then we compare the current value of |D_h| with all the markers.
  2065. Let |m| be the closest of these, and |mm| the corresponding rounded value.
  2066. Then we re-round |new_IM_h| to force it to lie on the `correct' side of |mm|.
  2067. This seems to work fairly often, but it does sometimes slip. First put the
  2068. markers on top of the stack...
  2069. @<Find the new position |new_IM_h|@>=
  2070.   begin s_top := stack_ht ;
  2071.     D_h_stack[s_top] := D_h_left;
  2072.     IM_h_stack[s_top] := IM_h_left;
  2073.     incr(s_top) ;
  2074.     D_h_stack[s_top] := D_h_mid;
  2075.     IM_h_stack[s_top] := IM_h_mid;
  2076.     incr(s_top) ;
  2077.     D_h_stack[s_top] := D_h_right;
  2078.     IM_h_stack[s_top] := IM_h_right;
  2079. @ ...then look for the stacked value closest to |D_h|...
  2080. @<Find the new position |new_IM_h|@>=
  2081.   m := s_top ;
  2082.   for n := s_top downto 1 do begin
  2083.     diff := D_h - D_h_stack[n] ;
  2084.     if abs(diff) <= abs(delta) then
  2085.     begin m := n ; delta := diff; end ;
  2086.   end;
  2087. @ ...then adjust |new_IM_h| by reference to this point on the stack.
  2088. @<Find the new position |new_IM_h|@>=
  2089.   if (delta > thin_space ) and ( new_IM_h < IM_h_stack[m] + gap_width) then
  2090.   new_IM_h := IM_h_stack[m] + gap_width
  2091.   else if (delta < -thin_space )
  2092.   and ( new_IM_h > IM_h_stack[m] - gap_width)
  2093.   then new_IM_h := IM_h_stack[m] - gap_width
  2094.   else if abs(delta) <= thin_space then new_IM_h := IM_h_stack[m];
  2095. @ We must assign values to these markers. When we start a page, all the
  2096. markers that were left over from the previous page are irrelevant. So we reset
  2097. them. This is a good place to consider margins. The standard arrangement given
  2098. in the \TeX book (Chapter 23) is that \.{DVI} point $(0,0)$ is about an inch
  2099. in from the top and left edges of the paper. But a negative {\tt \BS hoffset}
  2100. allows \.{DVI} to address points with negative coordinates, which should still
  2101. be on the paper. It seems that the least messy way to implement this is by
  2102. adding |l_margin| to |IM_h|, whenever this is set to an absolute value.
  2103. @.TeXbook@>@.Margins@>
  2104. @<Set up an empty page image@>=
  2105.   IM_h := @!l_margin ;
  2106.   IM_v := @!top_margin  ;
  2107.   D_h_left := 0 ; IM_h_left := l_margin ;
  2108.   D_h_mid := 0 ; IM_h_mid := l_margin ;
  2109.   D_h_right := 0 ; IM_h_right := l_margin ;
  2110. @ So now we consider the three arrows in turn. The left hand arrow will be
  2111. marked by |@!D_h_left|. It records the latest horizontal position to be
  2112. |push|ed. There might have been a |pop| since then, so it is not necessarily
  2113. the value at the top of the stack. If we just record |IM_h| whenever we
  2114. |push|, that would give a wrong value whenever there was a sequence
  2115. |push..move_right..push|. So we must rectify the pushed value of |IM_h|.
  2116. @ @<Some adjustments...@>=
  2117.   IM_h_stack[stack_ht]:=IM_h;
  2118.   IM_v_stack[stack_ht]:=IM_v;
  2119.   if just_pushed and (stack_ht > 0) then begin
  2120.     x := h_conv*(D_h_stack[stack_ht] - D_h_stack[stack_ht - 1] );
  2121.     if abs(x) > 1.5 {a guess!} then
  2122.     IM_h_stack[stack_ht] := IM_h_stack[stack_ht] + round(x) ;
  2123.   end;
  2124.   D_h_left := D_h ;
  2125.   IM_h_left := IM_h_stack[stack_ht] ;
  2126. @ The centre arrow will be marked by |@!D_h_mid|. This is defined as the value
  2127. of |D_h| just before setting the first character after the latest |push|.
  2128. @<Find the new position |new_IM_h|@>=
  2129.   if just_pushed then begin
  2130.     D_h_mid := D_h ;
  2131.     IM_h_mid := new_IM_h;
  2132.     just_pushed := false;
  2133.   end;
  2134. @ The right hand arrow is marked by |@!D_h_right|. At any time, this is
  2135. defined as the right hand edge of the latest previous character (or rule) that
  2136. has just been set. This equals |D_h + D_dis|, where |D_dis| is the \TeX\ width
  2137. of the character. Usually there will follow a |move_right| that updates |D_h|,
  2138. but |D_h_right| must be updated even if there is no |move_right|. Now
  2139. |@!IM_h_right| must be aligned with the right hand edge of the printed
  2140. representation of the character. The idea is that this will usually be the
  2141. exact place where the next character has to be set. We hope that all the
  2142. characters in each word will be correctly placed next to one another and the
  2143. accumulated drift will appear in spaces between the words. So whenever a
  2144. character is set, we must assign values to |D_dis| and |IM_dis|. The character
  2145. is described by |cod|, and its printed width is written into its |breadth|
  2146. field; but if it is a multiple character, then the |breadth| is the negative
  2147. of the width.
  2148. @<Do messy things...@>=
  2149.   D_dis := D_width[D_font, c_num] ;
  2150.   if cod.breadth = bad_char then IM_dis := 0
  2151.   else IM_dis := abs(cod.breadth) ;
  2152.   @<Set |rail_base|@>
  2153.   D_h_right := D_h + D_dis ;
  2154.   IM_h_right := IM_h + IM_dis ;
  2155. @ So the procedure |row| must give the |breadth| field the right value when
  2156. assembling a |multi| character. Recall that that character can be either an
  2157. |o_list| or an |h_list| or a |v_list|, and |c| tells us which it is. An
  2158. |o_list| is assumed to have a width of one |char_width| and the width of a
  2159. |v_list| is the width of its widest component. The width of a |h_list| gets
  2160. accumulated in |delta_h| as the list is assembled.
  2161. @<Copy |lig_buff|...@>=
  2162.   if c = o_bra then print_width := char_width
  2163.   else if c = h_bra then print_width := delta_h
  2164.   else begin
  2165.     print_width := char_width ;
  2166.     for i := 1 to buf_num do
  2167.     with lig_buff[2*i].code do
  2168.     if (print_width < -breadth ) and (breadth > -30000 )
  2169.     then print_width := -breadth  ;
  2170.   end;
  2171.   value.breadth := - print_width ;
  2172. @ @<Row locals...@>=
  2173.   print_width: integer ;
  2174. @ We must do the same thing when setting a rule.
  2175. @<|Post| set...@>=
  2176.   D_dis := D_rul_width ;
  2177.   IM_dis := hn * post_width ;
  2178.   D_h_right := D_h + D_dis ;
  2179.   IM_h_right := IM_h + IM_dis ;
  2180. @ @<|Rail| set...@>=
  2181.   D_dis := D_rul_width ;
  2182.   IM_dis := hn * rail_width ;
  2183.   D_h_right := D_h + D_dis ;
  2184.   IM_h_right := IM_h + IM_dis ;
  2185. @ \.{DVItype} handles vertical motion in the same sort of way as horizontal.
  2186. @d v_step_round(#) == round(v_conv * # )
  2187. @<Medium...@>=
  2188.   procedure move_down(ddd: integer);
  2189.   var new_IM_v , delta : integer;
  2190.   begin
  2191.     D_v:=D_v+ddd;
  2192.     delta := v_step_round(ddd) ;
  2193.     @<Find a vertical position |new_IM_v|@>
  2194.   end;
  2195. @ @<Find a vert...@>=
  2196.   if delta >= big_drop then begin
  2197.     new_IM_v := v_step_round(D_v) + top_margin ;
  2198.     if new_IM_v < IM_v + big_drop then
  2199.     IM_v := IM_v + big_drop
  2200.     else IM_v := new_IM_v ;
  2201.     rail_base := IM_v * rail_types ;
  2202.   end
  2203.   else if delta <= -big_drop then begin
  2204.     new_IM_v := v_step_round(D_v) + top_margin ;
  2205.     if new_IM_v > IM_v - big_drop then
  2206.     IM_v := IM_v - big_drop
  2207.     else IM_v := new_IM_v ;
  2208.     rail_base := IM_v * rail_types ;
  2209.   end else
  2210. @ The above calculation fails for small motions. Because \TeX\ expects
  2211. subscripts to be about half the size of the main line, it drops them by only a
  2212. small amount; with a crude printer, this small amount gets rounded to zero. If
  2213. the move is smaller than |@!tiny_drop| \.{DVI} units, we ignore it. If not,
  2214. then we force the new value of |IM_v| to be different from the old.
  2215. @<Find a vert...@>=
  2216.   begin
  2217.     IM_v := IM_v + delta ;
  2218.     rail_base := rail_base +  v_step_round(ddd * rail_types) ;
  2219.     if (ddd >  tiny_drop) and ( delta = 0) then IM_v := IM_v + 1
  2220.     else if (ddd < -tiny_drop) and ( delta = 0) then IM_v := IM_v - 1
  2221.     else rail_base := IM_v * rail_types ;
  2222.   end;
  2223. @ The next bit is put in to help catch bugs. Sometimes the \.{DVI} file tries
  2224. to address an absurd position; for example, I contrived to make \TeX\ generate
  2225. a {\tt \BS hbox} that was 9000 points wide. If we do nothing about this,
  2226. \.{Crudetype} will probably crash with an arithmetic error, which is
  2227. unacceptable. So any character falling outside the limits |h_min..h_max| and
  2228. |v_min..v_max| will generate an error report.
  2229. @<Check the position@>=
  2230.   if (Set_h < h_min) or ( Set_h > h_max )
  2231.   then begin
  2232.     warn('out of bounds position') ;
  2233.     Set_h := h_min ;
  2234.     {Chuck the character somewhere, hopefully out of the way}
  2235.   end;
  2236.   if (Set_v < v_min) or ( Set_v > v_max )
  2237.   then begin
  2238.     warn('out of bounds position') ;
  2239.     Set_v := v_min ;
  2240.   end;
  2241.     @.error: out of bounds@>
  2242. @ @<Set init...@>=
  2243.   h_max := h_resolution * 100 ;
  2244.   v_max := v_resolution * 100 ;
  2245.   h_min := -10 * h_resolution ;
  2246.   v_min := -10 * v_resolution ;
  2247. @ Note that since the position fields of a |page_record| are subranges,
  2248. |h_max| etc. must be of the same type.
  2249. @<Glob...@>= h_max, v_max , h_min, v_min : i_word ;
  2250. @* Sorting the page.
  2251. Once we have assembled the complete page image, we must sort it. The method
  2252. used here is a merge sort based on the country dance called Grand March.
  2253. @<Sort the page@>=
  2254.   @<The dancers form a long line up the middle of the hall and march
  2255.     up towards the Presence@>
  2256.   repeat
  2257.     @<At the top they split, and alternate groups go to the left and right and
  2258.       march down the sides@>
  2259.     @<At the bottom of the hall, each group coming from the right hand side
  2260.       merges with a group from the left side, and they go up again @>
  2261.   until sorted;
  2262. @ Since the data being sorted is of unpredictable size and sequentially
  2263. processed, it logically ought to be a |file|. But this turned out to make the
  2264. program spectacularly slow. So I use linked lists instead--- a sacrifice of
  2265. logic to economy. But I continue to use file-like language.
  2266. @d send_one_set_to( #)==
  2267.   copy_from( mid ) ( # )
  2268. @<At the top...@>=
  2269.   L_reset( mid) ;
  2270.   L_rewrite( left) ;
  2271.   L_rewrite( right) ;
  2272.   repeat
  2273.     send_one_set_to( left) ;
  2274.     if not L_eof( mid) then
  2275.     send_one_set_to( right) ;
  2276.   until L_eof( mid);
  2277. @ Eventually everybody comes together in one enormous set and the dance is
  2278. finished. The easiest way to detect this is to let it go round one more time.
  2279. Then the left side of the hall will be full and the right hand side empty.
  2280.  @<At the bottom...@>=
  2281.   L_rewrite( mid) ;
  2282.   L_reset( left) ;
  2283.   L_reset( right) ;
  2284.   sorted := L_eof( right) ;
  2285.   if sorted then
  2286.     page_ptr := son( next( left))
  2287.   else repeat
  2288.     if L_eof( right) then copy_from( left)  ( mid)
  2289.     else if L_eof( left) then  copy_from( right)  ( mid)
  2290.     else @<Merge one group from each side@>
  2291.   until L_eof( left) and L_eof( right) ;
  2292. @ The natural way to assemble the page image is to throw everything into one
  2293. huge list, then start sorting. But the code for merging two simple lists was
  2294. horribly complicated. (The code given here merely merges two runs.) So the
  2295. page image is a list of lists (another sacrifice of logic to economy). Each
  2296. top-level entry has a |son|, which points to a sub-list. This is a sorted
  2297. subset (a ``run'') of the data. One advantage of the list-of-lists structure
  2298. is that we can take advantage of the fact that \TeX\ output is very ``runny''.
  2299. I found that this made \.{Crudetype} run at least 3 times faster than before.
  2300. @d Add_run == new_tail( mid_ptr) ; son( mid_ptr) := run_ptr ;
  2301. @<Merge one group...@>=
  2302.   begin
  2303.     L_rewrite( run) ;
  2304.     L_run_ptr := son( left_ptr) ;
  2305.     R_run_ptr := son( right_ptr) ;
  2306.     repeat
  2307.       if @<The person on the left is more eligible@>
  2308.       then copy_from( L_run) ( run)
  2309.       else copy_from( R_run) ( run) ;
  2310.     until L_eof( R_run) and  L_eof( L_run) ;
  2311.     step_wipe( left_ptr) ;
  2312.     step_wipe( right_ptr) ;
  2313.     L_reset( run) ;
  2314.     Add_run ;
  2315.   end;
  2316. @ So while the page image is being assembled, it must be divided into runs.
  2317. @<Add the record...@>=
  2318.   begin
  2319.     if out_of_sequence then begin {create a new run}
  2320.       L_reset( run) ;
  2321.       Add_run ;
  2322.       L_rewrite( run) ;
  2323.     end;
  2324.     new_tail( run_ptr ) ;
  2325.     with image( run_ptr) do begin {write the data into it}
  2326.       hpos := Set_h ; Old_h := Set_h ;
  2327.       vpos := Set_v ; Old_v := Set_v ;
  2328.       data := cod ;
  2329.     end;
  2330.     incr(page_size) ;
  2331.     if page_size >= page_max then abort(
  2332.       'overflowed page: either a bug, or recompile with larger page_max' ) ;
  2333.   end
  2334.     @.Fatal: overflowed page@>
  2335. @ Once the lists are all assembled, we must |reset| them before sorting.
  2336. @<The dancers...@>=
  2337.   sorted := false;
  2338.   L_reset( run) ;
  2339.   Add_run ;
  2340. @ Now we must specify the desired order!! That is: increasing |vpos| and
  2341. |hpos|, |vpos| is more significant.
  2342. @d out_of_sequence ==
  2343.   ( ( Old_v > Set_v) or ( ( Old_v = Set_v) and ( Old_h > Set_h)))
  2344. @<The person on the left is more eligible@>=
  2345.   ( ( image( L_run_ptr).vpos < image( R_run_ptr).vpos) or
  2346.     ( ( image( L_run_ptr).vpos = image( R_run_ptr).vpos)
  2347.       and ( image( L_run_ptr).hpos <= image( R_run_ptr).hpos)))
  2348. @ And here we get it all started. Since |garbage| wipes out everything in the
  2349. |pool| array above |zzz|, the following code effectively makes |mid..run|
  2350. permanent.
  2351. @<Set init...@>=
  2352.   first_cell ;
  2353.   make_new( mid );
  2354.   make_new( left );
  2355.   make_new( right );
  2356.   make_new( run );
  2357.   make_new( zzz );
  2358.   image(zzz).vpos := max_half;
  2359.   next(zzz) := zzz ;
  2360.   mid_ptr := zzz ;
  2361.   run_ptr := zzz ;
  2362. @ @<Set up an empty page image@>=
  2363.   garbage ;
  2364.   L_rewrite( mid) ;
  2365.   L_rewrite( run) ;
  2366.   page_size := 0 ;
  2367.   Old_v := -max_half ;
  2368. @ @<Glob...@>=
  2369.   zzz, cell, tempp, page_ptr,
  2370.   mid, mid_ptr, run, run_ptr,
  2371.   left, left_ptr, L_run_ptr ,
  2372.   right, right_ptr, R_run_ptr : link;
  2373.   page_size: page_i ;
  2374.   Old_v, Old_h : i_word ;
  2375.   sorted: boolean ;
  2376.   declare_pool
  2377. @ Now we must define lots of machinery for handling lists. We could represent
  2378. a list by either a big array or dynamic storage. Neither is ideal, because an
  2379. array is bound to be either too big or too small; and some \.{PASCAL}s
  2380. apparently do not implement pointers. So I have expressed everything in terms
  2381. of certain macros, defined in the system dependent part of the program. In
  2382. theory, you can switch \.{Crudetype} from array to heap merely by redefining
  2383. these as follows:
  2384. \begintt
  2385.     define image(#) == #^
  2386.     define create == new(cell)
  2387.     define first_cell ==
  2388.     define link_type == ^page_record
  2389.     define wipe_out(#) == dispose(#) ; { release data piecemeal}
  2390.     define garbage ==
  2391.     define declare_pool ==
  2392. \endtt
  2393. Both array and heap seem to work in VMS. I prefer to use an array because in
  2394. VMS, there seems to be no shortage of store, and an array is easier to debug.
  2395. Assuming these lowest-level macros, here is some machinery for handling lists.
  2396. We must deallocate cells after use. When using arrays, the |garbage| command
  2397. does it all in one go. Pointers must be |dispose|d one at a time, and the
  2398. obvious time is just after the data was used.
  2399. @d next(#) == image(#).prox
  2400. @d advance(#) == # := next(#)
  2401. @d make_new( #) == create; # := cell ;
  2402. @d new_tail( #) ==
  2403.   create; next( #) := cell; # := cell ;
  2404. @d step_wipe( #) ==
  2405.    tempp := # ; advance( #) ; wipe_out( tempp)
  2406. @ Suppose |L| is a list; then the actual variable |L| points to a permanently-
  2407. allocated cell which in turn points to the head of the list. |L_ptr| points to
  2408. the active end. After the list has been assembled, we first mark the tail, by
  2409. attaching a special element called |zzz|. Then we move the |L_ptr| round to
  2410. the head. |copy_from| must be used in the combination
  2411. {\tt copy\_from(A) ( B)}. It copies one element from the head of |A| to the
  2412. tail of |B|.
  2413. @d L_rewrite( #) ==
  2414.   #@&ptr := # ; next( #) := zzz
  2415. @d L_reset( #) ==
  2416.   next ( # @& ptr) := zzz ; #@&ptr := next( #)
  2417. @d L_eof( #) ==
  2418.   ( # @& ptr = zzz)
  2419. @d copy_end( #) ==
  2420.   next( #@&ptr) := tempp ; advance( #@&ptr) ; end
  2421. @d copy_from( #)==
  2422. begin
  2423.   tempp := #@&ptr ;
  2424.   advance( #@&ptr ) ;
  2425.   copy_end
  2426. @ Each top-level entry has the |false| type below; the |prox| field points to
  2427. the next top-level entry and the |down| field to a sub-list.
  2428. @d son(#) == image(#).down
  2429. @<Types...@>=
  2430.   page_i = 0..page_max ;
  2431.   link = link_type ;
  2432.   page_record = packed record
  2433.     prox: link ;
  2434.     case boolean of
  2435.       true: ( hpos : i_word;
  2436.         vpos: i_word;
  2437.         data: code_object ) ;
  2438.       false: ( down : link) ;
  2439.     end;
  2440. @* Processing a page of output.
  2441. The output of \.{Crudetype} is done by the procedure |Send_page|, which takes
  2442. the page and translates it for the printer. We shall process it a `line' at a
  2443. time, meaning all |page_records| with the same |vpos|. Initially |PR_font|
  2444. gets an impossible value so as to force an explicit |set_PR_font|.
  2445. @<Top level...@>=
  2446.   procedure Send_page;
  2447.   var line: link ;
  2448.   begin
  2449.     @<Pause reset@>;
  2450.     PR_font := sentry;
  2451.     PR_h := 0;
  2452.     PR_v := 0;
  2453.     repeat
  2454.       line := read_line ;
  2455.       do_line(line);
  2456.     until L_eof( page) ;
  2457.   end;
  2458. @ The function |read_line| runs along the page image until the vertical
  2459. position changes. It returns a pointer to a sublist which is the next line on
  2460. the page. As side effects, it moves the printer into position for this line,
  2461. advances |page_ptr| to the first record of the next line, and updates
  2462. |PR_v| and |PR_v_next|.
  2463. @<Medium...@>=
  2464.   function read_line : link ;
  2465.   var head, tail: link ; size: integer;
  2466.   begin
  2467.     head := page_ptr ;
  2468.     size := 0 ;
  2469.     PR_v_next := image(page_ptr).vpos ;
  2470.     @<Move printer vertically to |PR_v_next|, update |PR_v| @>;
  2471.     repeat
  2472.       tail := page_ptr ;
  2473.       advance(page_ptr) ;
  2474.       PR_v_next := image(page_ptr).vpos ;
  2475.       incr(size) ;
  2476.       if size = max_line_size then
  2477.       warn('excessively long line, probably this is a bug') ;
  2478.         @.error: excessively long line@>
  2479.     until ( ( L_eof( page) )
  2480.       or (PR_v_next <> PR_v ) ) ;
  2481.     next(tail) := zzz ;
  2482.     read_line := head ;
  2483.   end;
  2484. @ These variables all denote the printer fonts, etc.
  2485. @<Glob...@>=
  2486.   PR_v, PR_v_next,
  2487.   PR_h, PR_h_next,
  2488.   PR_font : i_word ;
  2489. @ This procedure tries to print a line. The main difficulties are: we dont
  2490. want to |Backfeed| unless absolutely necessary; and we may have to deal with
  2491. overstruck characters. One possible way is to shunt them aside somewhere, then
  2492. print the |overflow| after the main line has been printed.
  2493. @<Medium...@>=
  2494.   procedure do_line (line_ptr: link);
  2495.   var overflow : link;
  2496.   begin
  2497.     overflow := zzz ;
  2498.     while line_ptr<>zzz do
  2499.     @<Process the character that |line_ptr| points to, and |advance| to
  2500.       the next@>;
  2501.     @<End the line, trying very hard not to over-feed the paper and print
  2502.       the |overflow|@>;
  2503.     @<Check pause@>;
  2504.   end;
  2505. @ We are actually getting almost in sight of the printer!!! Before we can
  2506. actually print a character, we must first check if it has to go to the
  2507. |overflow|...
  2508. @<Process the char...@>=
  2509.   with image(line_ptr) do begin
  2510.     PR_h_next := hpos ;
  2511.     if not b_space_absolute and not b_space_by_string and
  2512.     (PR_h_next < PR_h) then
  2513.     begin
  2514.       {AKT: ignore overflow stuff
  2515.       next(overflow) := line_ptr ;
  2516.       advance(overflow) ;
  2517.       AKT}
  2518.       advance(line_ptr) ;
  2519.     end
  2520.     else begin
  2521.       @<Set horizontal position for the next character@>;
  2522.       if  data.IM_font <> PR_font then set_PR_font(data.IM_font);
  2523.       print(zchr(data.IM_char )) ;
  2524.       PR_h := PR_h + data.breadth ;
  2525.       step_wipe(line_ptr ) ;
  2526.     end ;
  2527.   end;
  2528. @* Downloading. Not started yet.
  2529. @<Download a whole font@>= do_nothing
  2530. @ @<Enter a download...@>=
  2531. @* Carriage control.
  2532. Once the superior software has decided where the printer has to move to next,
  2533. this section has the job of translating the desired position into elementary
  2534. printer commands. Clearly this mapping depends very much on the range of
  2535. functions that the printer can perform. So this section is controlled by
  2536. several boolean constants; each asserts that the printer can do the
  2537. corresponding action. Here is a list of the most important ones:\item
  2538. |@!c_r_feed_dist| is the distance in |v_steps| by which a carriage-return
  2539. feeds the paper.\item
  2540. |@!w_l_feed_dist| ditto, |write_ln|. Similarly for the other |dist|
  2541. values.\item
  2542. |@!feed_absolute| says the printer has an absolute position command that takes
  2543. a parameter |IM_y|, say, and moves to position |IM_y v_steps| down the
  2544. page.\item
  2545. |@!b_feed_absolute| ditto, backfeeding.\item
  2546. |@!b_feed_by_string| says the printer has a |Backfeed| character that moves it
  2547. back by a fixed number |b_feed_dist| of |v_steps|. These booleans should not
  2548. be set true unless the printer can backfeed reliably.\item
  2549. |@!space_absolute| etc., Ditto, horizontal moves.\item
  2550. |@!abs_is_incr| says that in the absolute position commands, the parameter is
  2551. actually an incremental move.\item
  2552. |@!w_l_does_c_r| says that |write_ln| forces a carriage return.
  2553. As mentioned above, it is essential to avoid premature line feeds as much as
  2554. possible. Also, many operating systems will choke if the output record gets
  2555. too long, so we must do a |print_ln| at intervals. This program tries to
  2556. accommodate various types of carriage control, some of which are not in use at
  2557. the author's site. This means that several pieces of code have not been
  2558. tested. Installers may find that the procedures defined here will need to be
  2559. carefully studied in conjunction with the I/O section of their \PASCAL\
  2560. manual.
  2561. @<Const...@>= @<Carriage control constants@>
  2562. @ Now consider what happens at the end of each line. We will want to do a
  2563. subset of the following things: carriage-return, print the |overflow|, line
  2564. feed, split output records. We must keep a clear separation between these
  2565. tasks, and we want to do them in the stated order (but we cannot if
  2566. |fortran|). This order puts most of the carriage controls to the ends of the
  2567. output records, and (on our machine) makes it easier to examine the output
  2568. file with an editor. So first: do we want to do  carriage-return? If so, then
  2569. the natural way is to print a carriage-return, but not if it will over-feed
  2570. the paper.
  2571. @<End the line...@>=
  2572.   if  not w_l_does_c_r      {Return is compulsory}
  2573.   or (c_r_feed_dist = 0)   {Return is harmless  }
  2574.   or b_space_absolute
  2575.   or ((not want_split or (overflow <> zzz ) ){We can choose C-R or W-L}
  2576.     and (c_r_feed_dist < w_l_feed_dist))
  2577.   then begin
  2578.     if b_space_absolute and ((c_r_feed_dist > 0) or (l_margin > 0)) then
  2579.     set_h_abs(0)
  2580.     else begin
  2581.       if fortran then print_ln ;
  2582.       if not list then             {AKT: only send CR if not list}
  2583.       print(c_r_char);
  2584.       PR_h := 0;
  2585.       PR_v := PR_v + c_r_feed_dist ;
  2586.     end;
  2587. @ Now for the |overflow|. We will split records if that is harmless.
  2588. @<End the line...@>=
  2589.   if overflow <> zzz then begin
  2590.     if not fortran and (w_l_feed_dist = 0) then print_ln ;
  2591.                                            {AKT: must do this if list???}
  2592.     next(overflow) := zzz ;
  2593.     overflow := next(zzz) ;
  2594.     do_line(overflow);
  2595.   end;
  2596.   @<Reset printer at end of line, if necessary @>
  2597. @ @<Reset printer...@>= {hook}
  2598. @ Now we decide whether to do any |line_feed|s. But first, we may have to
  2599. attempt to |Backfeed|. Sometimes the program will fail; it should not do so
  2600. unless the \.{DVI} file calls for overstruck characters and the printer
  2601. genuinely cannot do them. If |b_feed_scream|, then print an error message.
  2602. @<Move printer...@>=
  2603.   if want_split then PR_v_next :=  PR_v_next - w_l_feed_dist ;
  2604.   if (PR_v_next < PR_v) then begin
  2605.     if b_feed_absolute then set_v_abs(PR_v_next)
  2606.     else if b_feed_by_string then
  2607.     while PR_v_next < PR_v do @<Backfeed@>
  2608.     else if b_feed_scream then begin
  2609.       warn('this printer cant feed backwards');
  2610.         @.error: printer cant...@>
  2611.       display_ln('approximate vertical position is: ', PR_v_next);
  2612.       display_ln(' printing over-fed line on line below');
  2613.       display_ln(' ');
  2614.       PR_v := PR_v_next;
  2615.     end;
  2616.   end;
  2617. @ If we avoided over-feeding, we may want to feed forwards.
  2618. @<Move printer...@>=
  2619.   if PR_v_next > PR_v then begin
  2620.     if feed_absolute then set_v_abs(PR_v_next)
  2621.     else begin
  2622.       while PR_v_next >= PR_v + feed_dist do @<Line feed@>;
  2623.       while PR_v_next > PR_v do @<Tiny feed@> ;
  2624.     end;
  2625.   end;
  2626.   if want_split then begin
  2627.     PR_v := PR_v + w_l_feed_dist ;
  2628.     PR_v_next := PR_v_next + w_l_feed_dist ;
  2629.     if not list then print_ln;                {AKT: was just print_ln;}
  2630.     if w_l_does_c_r then PR_h := 0 ;
  2631.   end;
  2632. @ We set the horizontal position in a similar way, but we do not need to be so
  2633. paranoid about backspacing as about back-feeding.
  2634. @<Set horiz...@>=
  2635.   if PR_h_next = PR_h then
  2636.   else begin
  2637.     if  (PR_h_next < PR_h) then begin
  2638.       if b_space_absolute then set_h_abs(PR_h_next)
  2639.       else if b_space_by_string then
  2640.       while PR_h_next < PR_h do @<Backspace@>;
  2641.     end;
  2642.     if space_absolute and (PR_h_next > PR_h )
  2643.       then set_h_abs(PR_h_next)
  2644.     else begin
  2645.       while PR_h_next >= PR_h + space_dist do @<Space@>;
  2646.       while PR_h_next > PR_h do @<Tiny space@> ;
  2647.     end;
  2648.   end;
  2649. @* Low level modules for printer control.
  2650. Now we have to translate these elementary printer commands into actual strings
  2651. of characters to be put into |printfile|. Here is the command for setting a
  2652. new printer's font.
  2653. @<Lowest...@>=
  2654.   procedure set_PR_font(new:integer) ;
  2655.   begin
  2656.     if (new = PR_font) or only_one_font then
  2657.     else
  2658.     begin
  2659.       string_print(font_prefix) ;
  2660.       print_integer(new, param_type);
  2661.       string_print(font_suffix) ;
  2662.       PR_font := new ;
  2663.     end;
  2664.   end;
  2665. @ Now for |absolute| movements, if the printer can do them. The procedure
  2666. |set_v_abs| moves the printer to position |mm h_steps| below the top of the
  2667. paper. If |abs_is_incr| then the printers `absolute' command is actually an
  2668. incremental command. So the parameter sent to the printer must be decreased by
  2669. |PR_v|.
  2670. @<Forward...@>=
  2671.   procedure set_v_abs(mm: integer) ; forward ;
  2672.   procedure set_h_abs(mm: integer) ; forward ;
  2673. @ @<Lowest...@>=
  2674.   procedure set_v_abs;
  2675.   var new_pos :integer ;
  2676.   begin
  2677.     if abs_is_incr then
  2678.     new_pos := mm - PR_v
  2679.     else new_pos := mm ;
  2680.     string_print (v_abs_prefix) ;
  2681.     print_integer(new_pos, param_type) ;
  2682.     string_print (v_abs_suffix) ;
  2683.     PR_v := mm ;
  2684.   end;
  2685.   procedure set_h_abs;
  2686.   var new_pos :integer ;
  2687.   begin
  2688.     if abs_is_incr then
  2689.     new_pos := mm - PR_h
  2690.     else new_pos := mm ;
  2691.     string_print (h_abs_prefix) ;
  2692.     print_integer(new_pos, param_type) ;
  2693.     string_print (h_abs_suffix) ;
  2694.     PR_h := mm ;
  2695.   end;
  2696. @ Now consider commands for printers that can only do simple movements. A
  2697. |tiny| movement is usually a movement of one |h_step| or |v_step|. All these
  2698. modules should be protected, so they cannot be called unless the printer can
  2699. actually do the stated movement. Normally, the command strings for these are
  2700. only simple characters, so we can just |print| them.
  2701. @<Formfeed@>=
  2702.   {AKT: was page(printfile);}
  2703.      print_ln;
  2704.      print_ln;
  2705.      print_ln;
  2706.      print('-------------------- [new page] --------------------');
  2707.      print_ln;
  2708.      print_ln;
  2709.   if is_header then
  2710.   string_print (page_top);
  2711. @ @<Backfeed@>=
  2712.   begin string_print(b_feed_string);
  2713.     PR_v:=PR_v - b_feed_dist;
  2714.   end
  2715. @ @<Line feed@>=
  2716.   begin
  2717.     if fortran then begin
  2718.       print_ln ;
  2719.       PR_v := PR_v + w_l_feed_dist;
  2720.     end;
  2721.     if list then           {AKT: was just print(feed_char);}
  2722.        print_ln
  2723.     else
  2724.        print(feed_char);
  2725.     PR_v:=PR_v+feed_dist;
  2726.   end;
  2727. @ @<Tiny feed@>=
  2728.   begin print(t_feed_char); PR_v:=PR_v+t_feed_dist; end;
  2729. @ @<Backspace@>=
  2730.   begin print (b_space_char); PR_h:=PR_h-b_space_dist; end;
  2731. @ @<Space@>=
  2732.   begin print (space_char); PR_h:=PR_h+space_dist; end;
  2733. @ @<Tiny space@>=
  2734.   begin print (t_space_char); PR_h:=PR_h+t_space_dist; end;
  2735. @* Default declarations for printer.
  2736. Here we define a lot of printer-dependent material that is expected to be the
  2737. same for most printers. Of course, these will have to be changed if |fortran|,
  2738. or on a system that does not use ASCII codes. First, some command characters
  2739. for simple movements. |feed| means a vertical movement and |space| horizontal.
  2740. Each |thing_char| is the character needed to make the printer do the named
  2741. action. Owing to the rules of \.{TANGLE}, the words |back| and |tiny| have to
  2742. be abbreviated (to avoid identifier clashes). |c_r_char| etc. must be
  2743. consistent with the value of |fortran|.
  2744.   @.ASCII@>
  2745. @<Set init...@>=
  2746.   space_char := chr(32) ;
  2747.   t_space_char := chr(32) ;
  2748.   feed_char := chr(10) ;
  2749.   t_feed_char := chr(10);
  2750.   c_r_char := chr(13);
  2751.   b_space_char := chr(8);
  2752. @ @<Glob...@>=
  2753.   space_char,
  2754.   t_space_char,
  2755.   feed_char ,
  2756.   t_feed_char ,
  2757.   c_r_char ,
  2758.   b_space_char : char ;
  2759. @ Next the distances that they normally move, always in |steps|.
  2760. @<Carriage control const...@>=
  2761.   space_dist = 1;
  2762.   b_space_dist = 1;
  2763.   t_space_dist = 1;
  2764.   feed_dist = 1 ;
  2765.   w_l_feed_dist = 0 ;
  2766.   b_feed_dist = 0;
  2767.   t_feed_dist = 1;
  2768.   c_r_feed_dist = 0 ;
  2769.   tiny_drop = 500000 ;  {AKT: avoid E in TEX dropping to next line}
  2770.   {tiny_drop = 50000 ;   slightly less than a point}
  2771.   big_drop = 4 ;
  2772. @ |start_stuff|  and |stop_stuff| get written into the start and end of
  2773. |printfile|. They are intended to: set printer into correct state for \TeX\
  2774. output, and reset printer to standard state afterwards. If the printer needs
  2775. to be re-initialised in any way at the top of each page, then set |@!page_top|
  2776. to the necessary data and set |is_header| to |true|.
  2777. @<Open |printfile|@>=
  2778.   string_print(start_stuff) ;
  2779.   print_ln ;
  2780. @ @<Clean up afterwards@>=
  2781.   string_print(stop_stuff);
  2782. @ @<Glob...@>=
  2783.   start_stuff,
  2784.   stop_stuff,
  2785.   page_top,
  2786.   b_feed_string ,
  2787.   font_prefix,
  2788.   font_suffix,
  2789.   v_abs_prefix,
  2790.   v_abs_suffix,
  2791.   h_abs_prefix,
  2792.   h_abs_suffix : var_string ;
  2793.   print_end : var_string ;
  2794. @* Printer dependent data.
  2795. This section should define masses of data to describe how the printer behaves.
  2796. In order to keep the size of each printer's change file within reasonable
  2797. bounds, I have replaced this section by a blank.  The missing data is given in
  2798. the line printer change file. To set up for another printer, that file will
  2799. have to be extensively edited.
  2800. *** Attach printer change file here ***
  2801. @* Index.
  2802. Pointers to error messages appear here together with the section numbers
  2803. where each identifier is used.
  2804.